typeguards.json module

TypeGuard functions for JSON and JSON schemas.

typeguards.json.JSON

JSON type.

alias of Dict[str, str | int | float | bool | None | List[JSONValue] | Dict[str, JSONValue]]

typeguards.json.JSONValue

JSON value type defined recursively.

alias of str | int | float | bool | None | List[JSONValue] | Dict[str, JSONValue]

typeguards.json.Schema

Schema type.

A schema type must describe the keys and values in a Dict, and can be:

The types of the keys in a schema can also be any of the preceding schema types, as well as any of these addtional schema types:

  • a generic union of JSON values, or any of the schema types

  • a generic List of JSON values, or any of the schema types

  • a List of JSON values

  • a simple JSON value: str | int | float | bool | None

Todo

  • Replace general type definition to one that conforms to schema types

    • Perhaps have a Protocol, Annotated, for an object that has annotations

alias of type | UnionType | Tuple[type | UnionType, …]

typeguards.json.is_json(value: object) TypeGuard[Dict[str, str | int | float | bool | None | List[str | int | float | bool | None | List[JSONValue] | Dict[str, JSONValue]] | Dict[str, str | int | float | bool | None | List[JSONValue] | Dict[str, JSONValue]]]][source]

Type narrow an object to JSON.

Perform a deep check on a Dict to ensure that each key is a str, and each value is a JSONValue.

Parameters:

value – The object to narrow.

Returns:

True if value is a Dict of str keys and JSON values.

typeguards.json.is_json_schema(value: object, schema: type | UnionType | Tuple[type | UnionType, ...]) TypeGuard[type | UnionType | Tuple[type | UnionType, ...]][source]

Type narrow an object to a JSON schema.

Perform a deep check on a Dict to ensure that it conforms to schema. If schema specifies the keys that may exist in the Dict, and types for their values, then the Dict may not contain any keys not in schema. Each value in the Dict must also be an instance of the type associated with its key in schema. Note that the Dict does not have to contain every key defined in schema.

Parameters:
  • value – The object to narrow. This must be a Dict.

  • schema – An object that describes the schema for a Dict.

Returns:

True if value is a Dict that conforms to schema.

typeguards.json.is_json_value(value: object) TypeGuard[str | int | float | bool | None | List[str | int | float | bool | None | List[JSONValue] | Dict[str, JSONValue]] | Dict[str, str | int | float | bool | None | List[JSONValue] | Dict[str, JSONValue]]][source]

Type narrow an object to JSONValue.

Perform a deep check on an object to ensure that it is a JSON value.

Parameters:

value – The object to narrow.

Returns:

True if value is a JSON value.