Bundled formats

numbers-range

Format: numbers-range

Validation function: jsonschema_extras.formats.is_numbers_range()

Exceptions interpreted as format violations: TypeError.

Validation function
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

slice-string

Format: slice-string

Validation function: jsonschema_extras.formats.is_slice_string()

Exceptions interpreted as format violations: TypeError, ValueError.

Validation function
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'>