Skip to content

Validators

float_validator(value, context)

Checks if the value can be parsed as a float and returns it if it can, otherwise raises Invalid. Rejects NaN and inf.

Parameters:

Name Type Description Default
value

the value

required
context

the context

required

Returns:

Type Description
float

a float

Source code in ckanext/versioned_datastore/logic/validators.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def float_validator(value, context) -> float:
    """
    Checks if the value can be parsed as a float and returns it if it can, otherwise
    raises Invalid. Rejects NaN and inf.

    :param value: the value
    :param context: the context
    :returns: a float
    """
    try:
        value = float(value)
        if isfinite(value):
            return value
    except (ValueError, TypeError):
        pass
    raise toolkit.Invalid('Invalid float value')

url_safe(value, context)

Checks if the value is safe to be included in a URL as a slug.

Parameters:

Name Type Description Default
value

the value to check

required
context

the context in which to check

required
Source code in ckanext/versioned_datastore/logic/validators.py
58
59
60
61
62
63
64
65
66
67
68
69
70
def url_safe(value, context):
    """
    Checks if the value is safe to be included in a URL as a slug.

    :param value: the value to check
    :param context: the context in which to check
    """
    if not re.match('^[A-Za-z0-9-_]+$', value):
        raise toolkit.Invalid(
            'Only a-z, 0-9, hyphens (-) and underscores (_) are valid characters'
        )
    else:
        return value