jsonschema_extras.formats package¶
Utilities for working with format checkers, and some bundled format checkers.
To use some of the bundled format checkers:
instantiate a
jsonschema.FormatChecker(or use one which you already instantiate);add desired format checking functions to the format checker using
register_funcs_in_checker()orregister_func_in_checker()with bundled instances ofFormatCheckingFuncInfo;pass the format checker to
jsonschema.validate()orjsonschema.protocols.Validatorto enable validation of formats.
- class jsonschema_extras.formats.FormatCheckingFuncInfo(format, func, raises=())[source]¶
Bases:
NamedTupleData 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.
- 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:
- 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
slicesyntax:start:stop:stepwherestart,stop, andstepare optional integers that may be negative. At minimum, the string must contain at least one colon.- Return type:
- Returns:
Whether the string is a slice string (by the Python’s
slicesyntax).- 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:
checker (
FormatChecker) – Aformatproperty checker.func_info (
FormatCheckingFuncInfo) – Data needed to register a format checking function.
- Return type:
- Returns:
func_info.func
- jsonschema_extras.formats.register_funcs_in_checker(checker, funcs_info)[source]¶
Register multiple format checking functions in a
FormatChecker.- Parameters:
checker (
FormatChecker) – Aformatproperty checker.funcs_info (
Iterable[FormatCheckingFuncInfo]) – Instances of data needed to register formatt checking functions.
- Return type:
- Returns:
sequence of
func_info.funcobjects