Skip to content

Validators

in_list(list_possible_values)

Validator that checks that the input value is one of the given possible values.

Parameters:

Name Type Description Default
list_possible_values

function that returns list of possible values for validated field

required
Source code in ckanext/graph/logic/validators.py
37
38
39
40
41
42
43
44
45
46
47
48
49
def in_list(list_possible_values):
    """
    Validator that checks that the input value is one of the given possible values.

    :param list_possible_values: function that returns list of possible values for
        validated field
    """

    def validate(key, data, errors, context):
        if not data[key] in list_possible_values:
            raise toolkit.Invalid(f'"{data[key]}" is not a valid parameter')

    return validate

is_boolean(value, context)

Validate a field as a boolean. Assuming missing value means false.

Parameters:

Name Type Description Default
value
required
context
required
Source code in ckanext/graph/logic/validators.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def is_boolean(value, context):
    """
    Validate a field as a boolean. Assuming missing value means false.

    :param value:
    :param context:
    """

    if isinstance(value, bool):
        return value
    elif isinstance(value, str) and value.lower() in ['true', 'yes', 't', 'y', '1']:
        return True
    elif isinstance(value, str) and value.lower() in ['false', 'no', 'f', 'n', '0']:
        return False
    elif isinstance(value, type(toolkit.missing)):
        return False
    else:
        raise toolkit.Invalid(
            toolkit._(
                'Value must a true/false value (ie. true/yes/t/y/1 or false/no/f/n/0)'
            )
        )

is_date_castable(value, context)

Validator to ensure the date is castable to a date field.

Parameters:

Name Type Description Default
value
required
context
required
Source code in ckanext/graph/logic/validators.py
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def is_date_castable(value, context):
    """
    Validator to ensure the date is castable to a date field.

    :param value:
    :param context:
    """

    if value:
        fields = utils.get_datastore_field_types()

        field_type = fields[value]

        if field_type == 'date':
            return value
        else:
            script = """
            if (doc['data.{date_field_name}'].value != null) {{
             try {{
              new SimpleDateFormat('yyyy-MM-dd').parse(doc['data.{date_field_name}'].value);
              return true;
             }} catch (Exception e) {{
              return false;
             }}
            }} else {{
             return false;
            }}
            """.format(date_field_name=value)

            data_dict = {
                'search': {
                    'query': {
                        'bool': {'filter': {'script': {'script': {'source': script}}}}
                    }
                },
                'resource_id': toolkit.c.resource['id'],
            }

            failure = toolkit.Invalid(
                f"Field {value} cannot be cast into a date. Are you sure it's a date field?"
            )

            try:
                result = toolkit.get_action('datastore_search_raw')({}, data_dict)
                if result['total'] == 0:
                    raise failure
            except DataError:
                raise failure

    return value