Skip to content

Validators

string_max_length(max_length)

Checks if a string is longer than a certain length.

Parameters:

Name Type Description Default
max_length
required
Source code in ckanext/nhm/logic/validators.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def string_max_length(max_length):
    """
    Checks if a string is longer than a certain length.

    :param max_length:
    """

    def callable(value, context):
        """
        The validation function that checks if a string is longer than the max length.

        :param value: the input string
        :param context: the context
        """

        if len(value) > max_length:
            raise toolkit.Invalid(
                toolkit._(f'Length must be less than {max_length} characters')
            )

        return value

    return callable

uuid_validator(value, context)

Checks if a UUID is valid (used for MAM asset IDs). We check with a regex as MAM assets ids aren't valid UUIDs.

Parameters:

Name Type Description Default
value
required
context
required
Source code in ckanext/nhm/logic/validators.py
40
41
42
43
44
45
46
47
48
49
50
51
def uuid_validator(value, context):
    """
    Checks if a UUID is valid (used for MAM asset IDs). We check with a regex as MAM
    assets ids aren't valid UUIDs.

    :param value:
    :param context:
    """
    if uuid_re.match(value):
        return value
    else:
        raise toolkit.Invalid(toolkit._('Invalid Asset ID'))