Skip to content

Misc

validate_by_schema(context, data_dict, default_schema) ΒΆ

Validate the data_dict against a schema. If a schema is not available in the context (under the key 'schema') then the default schema is used.

If the data_dict fails the validation process a ValidationError is raised, otherwise the potentially updated data_dict is returned.

Parameters:

Name Type Description Default
context

the ckan context dict

required
data_dict

the dict to validate

required
default_schema

the default schema to use if the context doesn't have one

required
Source code in ckantools/validators/misc.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def validate_by_schema(context, data_dict, default_schema):
    """
    Validate the data_dict against a schema. If a schema is not available in the context
    (under the key 'schema') then the default schema is used.

    If the data_dict fails the validation process a ValidationError is raised, otherwise
    the potentially updated data_dict is returned.

    :param context: the ckan context dict
    :param data_dict: the dict to validate
    :param default_schema: the default schema to use if the context doesn't have one
    """
    schema = context.get('schema', default_schema)
    data_dict, errors = toolkit.navl_validate(data_dict, schema, context)
    if errors:
        raise toolkit.ValidationError(errors)
    return data_dict