protected function validate(favourite $favourite) {

        $favourite = (array)$favourite;

        // The allowed fields, and whether or not each is required to create a record.
        // The timecreated, timemodified and id fields are generated during create/update.
        $allowedfields = [
            'userid' => true,
            'component' => true,
            'itemtype' => true,
            'itemid' => true,
            'contextid' => true,
            'ordering' => false,
            'timecreated' => false,
            'timemodified' => false,
            'id' => false
        ];

        $requiredfields = array_filter($allowedfields, function($field) {
            return $field;
        });

        if ($missingfields = array_keys(array_diff_key($requiredfields, $favourite))) {
            throw new \moodle_exception("Missing object property(s) '" . join(', ', $missingfields) . "'.");
        }

        // If the record contains fields we don't allow, throw an exception.
        if ($unsupportedfields = array_keys(array_diff_key($favourite, $allowedfields))) {
            throw new \moodle_exception("Unexpected object property(s) '" . join(', ', $unsupportedfields) . "'.");
        }
    }