Skip to content

Validators

is_datastore_field(value, context) ΒΆ

Ensure field name exists in the resource datastore.

Parameters:

Name Type Description Default
value

field name

required
context

return:

required

Returns:

Type Description

the field name if it is valid, otherwise raises toolkit.Invalid error

Source code in ckanext/list/logic/validators.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def is_datastore_field(value, context):
    """
    Ensure field name exists in the resource datastore.

    :param value: field name
    :param context: return:
    :return: the field name if it is valid, otherwise raises toolkit.Invalid error
    :raises: toolkit.Invalid if the field name is invalid
    """
    existing_fields = get_datastore_fields(toolkit.c.resource['id'], context)
    if value:
        # there can just be one string or a list of strings
        fields = [value] if isinstance(value, str) else value
        # loop through values, making sure they're in the datastore
        if any(field not in existing_fields for field in fields):
            raise toolkit.Invalid(toolkit._('Field not found in datastore'))

    return value