ckanext-scheming icon indicating copy to clipboard operation
ckanext-scheming copied to clipboard

Delayed Validation

Open TkTech opened this issue 7 years ago • 3 comments

This is a simple but very useful trick we've done in an ongoing contract which allows fields to be marked as required only when the dataset is actually "published" (in this case set from private to public). This allows the minimal required metadata to be always required (title, slug, description, etc) and any number of other fields to be filled in over time by data entry without having to complete the entire form. However if a field is populated, then the other validators will still run on them.

Is this a desirable feature to have in ckanext-scheming?

    def get_validators(self):
        return {
            'require_when_published': self.required_validator
        }

    @staticmethod
    def required_validator(key, flattened_data, errors, context):
        """
        A custom required validator that prevents publishing if a required
        field is not provided.
        """
        is_private = flattened_data[('private',)]
        if not is_private:
            return get_validator('not_empty')(
                key,
                flattened_data,
                errors,
                context
            )

        return get_validator('ignore_missing')(
            key,
            flattened_data,
            errors,
            context
        )

    @staticmethod
    def field_required_helper(field):
        """
        Return field['required'] or guess based on validators if not present.
        """
        if 'required' in field:
            return field['required']

        validators = field.get('validators', '').split()

        # The standard scheming "required" validator.
        if 'not_empty' in validators:
            return True

        # Our custom validator to only require a field on publishing.
        if 'require_when_published' in validators:
            return True

TkTech avatar Aug 13 '18 01:08 TkTech

An implementation I'm working on right now requires this ability.

@TkTech @wardi just pinging if this will be added to ckanext-scheming

jqnatividad avatar Nov 24 '20 15:11 jqnatividad

You can just copy this into your extension, it's just a validator. This issue is just to see if it's worth including by default.

TkTech avatar Nov 24 '20 15:11 TkTech

+1 to adding this validator by default.

It has the added bonus of creating a simple workflow for newly inserted packages being marked private, helping with the curation process, whilst keeping the schema definition "clean" compared to defining a hidden field in the schema to flag deferred validation.

jqnatividad avatar Dec 03 '20 12:12 jqnatividad