1
# TODO: add validate regexp?
2

3

4 3
def validate_required(value, required):
5
    """Validate that ``value`` is set if ``required``
6

7
    Normally called in :meth:`~mopidy.config.types.ConfigValue.deserialize` on
8
    the raw string, _not_ the converted value.
9
    """
10 3
    if required and not value:
11 3
        raise ValueError("must be set.")
12

13

14 3
def validate_choice(value, choices):
15
    """Validate that ``value`` is one of the ``choices``
16

17
    Normally called in :meth:`~mopidy.config.types.ConfigValue.deserialize`.
18
    """
19 3
    if choices is not None and value not in choices:
20 3
        names = ", ".join(repr(c) for c in choices)
21 3
        raise ValueError(f"must be one of {names}, not {value}.")
22

23

24 3
def validate_minimum(value, minimum):
25
    """Validate that ``value`` is at least ``minimum``
26

27
    Normally called in :meth:`~mopidy.config.types.ConfigValue.deserialize`.
28
    """
29 3
    if minimum is not None and value < minimum:
30 3
        raise ValueError(f"{value!r} must be larger than {minimum!r}.")
31

32

33 3
def validate_maximum(value, maximum):
34
    """Validate that ``value`` is at most ``maximum``
35

36
    Normally called in :meth:`~mopidy.config.types.ConfigValue.deserialize`.
37
    """
38 3
    if maximum is not None and value > maximum:
39 3
        raise ValueError(f"{value!r} must be smaller than {maximum!r}.")

Read our documentation on viewing source code .

Loading