jsonschema_extras.formats package

Utilities for working with format checkers, and some bundled format checkers.

To use some of the bundled format checkers:

  1. instantiate a jsonschema.FormatChecker (or use one which you already instantiate);

  2. add desired format checking functions to the format checker using register_funcs_in_checker() or register_func_in_checker() with bundled instances of FormatCheckingFuncInfo;

  3. pass the format checker to jsonschema.validate() or jsonschema.protocols.Validator to enable validation of formats.

type jsonschema_extras.formats.FormatCheckFn = Callable[[object], bool]
class jsonschema_extras.formats.FormatCheckingFuncInfo(format, func, raises=())[source]

Bases: NamedTuple

Data needed to register a format checking function in a FormatChecker.

Parameters:
format

Exact name of the format for JSON Schema.

func

Function that checks if a JSON data value satisfies a format.

raises

Type(s) of exceptions raised by func on an invalid value. Exceptions of other types are immediately propagated. See checks() for details.

jsonschema_extras.formats.is_numbers_range(instance)[source]

Tests if an object is a numbers range (a sequence of 2 numbers in non-descending order).

A numbers range is defined as a sequence containing exactly 2 comparable numeric elements where the first element is less than or equal to the second element, forming a valid range [min, max].

Returns:

Whether instance is a numbers range: ascending sequence of 2 numbers.

Return type:

bool

Parameters:

instance (object)

Examples

Valid ranges:

>>> is_numbers_range([1, 5])
True
>>> is_numbers_range([3.14, 3.14])
True
>>> is_numbers_range((0, 10))
True

Invalid ranges:

>>> is_numbers_range([5, 1])
False
>>> is_numbers_range([1, 2, 3])
False
>>> is_numbers_range([1])
False
>>> is_numbers_range(5)
False
jsonschema_extras.formats.is_slice_string(instance)[source]

Tests if a string specifies a slice.

A slice string follows Python’s slice syntax: start:stop:step where start, stop, and step are optional integers that may be negative. At minimum, the string must contain at least one colon.

Return type:

bool

Returns:

Whether the string is a slice string (by the Python’s slice syntax).

Raises:

TypeError – If the given object is not a string.

Parameters:

instance (object)

Examples

Valid slice strings:

>>> is_slice_string('1:5')
True
>>> is_slice_string('::2')
True
>>> is_slice_string('-5:')
True
>>> is_slice_string(':')
True

Invalid slice strings:

>>> is_slice_string('1.5:10')
False
>>> is_slice_string('a:b')
False

Type checking:

>>> is_slice_string(123)
Traceback (most recent call last):
    ...
TypeError: str expected, got <class 'int'>
jsonschema_extras.formats.register_func_in_checker(checker, func_info)[source]

Register a format checking function in a FormatChecker.

Utility function working with FormatCheckingFuncInfo.

Parameters:
Return type:

Callable[[object], bool]

Returns:

func_info.func

jsonschema_extras.formats.register_funcs_in_checker(checker, funcs_info)[source]

Register multiple format checking functions in a FormatChecker.

Parameters:
Return type:

Sequence[Callable[[object], bool]]

Returns:

sequence of func_info.func objects

Submodules