Skip to content

Validators

colour_validator(value, context)

Validate a value is a CSS hex color.

Parameters:

Name Type Description Default
value

the value to validate

required
context

the context within which this validation is taking place

required

Returns:

Type Description

the validated value

Source code in ckanext/tiledmap/lib/validators.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def colour_validator(value, context):
    """
    Validate a value is a CSS hex color.

    :param value: the value to validate
    :param context: the context within which this validation is taking place
    :returns: the validated value
    """
    if re.match('^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$', value):
        if value[0] != '#':
            return f'#{value}'
        else:
            return value
    else:
        raise toolkit.Invalid(
            toolkit._(
                'Colors must be formed of three or six RGB hex value, '
                'optionally preceded by a # sign (eg. #E55 or #F4A088)'
            )
        )

float_01_validator(value, context)

Validates that the value is a float number between 0 and 1.

Parameters:

Name Type Description Default
value

the value

required
context

the context within which this validation is taking place

required

Returns:

Type Description

the validated value

Source code in ckanext/tiledmap/lib/validators.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def float_01_validator(value, context):
    """
    Validates that the value is a float number between 0 and 1.

    :param value: the value
    :param context: the context within which this validation is taking place
    :returns: the validated value
    """
    try:
        value = float(value)
    except ValueError:
        raise toolkit.Invalid(toolkit._('Must be a decimal number, between 0 and 1'))
    if value < 0 or value > 1:
        raise toolkit.Invalid(toolkit._('Must be a decimal number, between 0 and 1'))
    return value

is_datastore_field(value, context)

Check that the fields are indeed a datastore fields.

Parameters:

Name Type Description Default
value

the value to validate

required
context

the context within which this validation is taking place

required

Returns:

Type Description

the value

Source code in ckanext/tiledmap/lib/validators.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def is_datastore_field(value, context):
    """
    Check that the fields are indeed a datastore fields.

    :param value: the value to validate
    :param context: the context within which this validation is taking place
    :returns: the value
    """
    passed_fields = value if isinstance(value, list) else [value]
    fields = get_resource_datastore_fields(toolkit.g.resource['id'])
    invalid_fields = [field for field in passed_fields if field not in fields]
    if invalid_fields:
        raise toolkit.Invalid(f'Invalid parameters: {",".join(invalid_fields)}')
    return value

is_view_id(value, context)

Ensure this is a view id on the current resource.

Parameters:

Name Type Description Default
value

the value to validate

required
context

the context within which this validation is taking place

required

Returns:

Type Description

the value

Source code in ckanext/tiledmap/lib/validators.py
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def is_view_id(value, context):
    """
    Ensure this is a view id on the current resource.

    :param value: the value to validate
    :param context: the context within which this validation is taking place
    :returns: the value
    """
    if value:
        data = {'id': toolkit.g.resource['id']}
        views = toolkit.get_action('resource_view_list')(context, data)
        if value not in [view['id'] for view in views]:
            raise toolkit.Invalid(toolkit._('Must be a view on the current resource'))
    return value