ckan / ckanext-scheming

Easy, shareable custom CKAN schemas
Other
83 stars 162 forks source link

Delayed Validation #189

Open TkTech opened 6 years ago

TkTech commented 6 years ago

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
jqnatividad commented 3 years ago

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

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

TkTech commented 3 years ago

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.

jqnatividad commented 3 years ago

+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.