koda_validate.serialization#

class SerializableErr(obj)#

Bases: ValidationErrBase

A validation error where you may simply want to produce a human-readable message.

obj#

The error contents you want to return.

to_json_schema(obj)#

Produces a Serializable from a Validator, Predicate or PredicateAsync that conforms to a valid JSON Schema.

Parameters:

obj (Union[Validator[Any], Predicate[Any], PredicateAsync[Any]]) – the Validator being described

Return type:

Dict[str, Union[None, int, str, bool, float, List[Union[None, int, str, bool, float, List[Serializable], Tuple[Serializable, ...], Dict[str, Serializable]]], Tuple[Union[None, int, str, bool, float, List[Serializable], Tuple[Serializable, ...], Dict[str, Serializable]], ...], Dict[str, Union[None, int, str, bool, float, List[Serializable], Tuple[Serializable, ...], Dict[str, Serializable]]]]]

Returns:

a Serializable compatible with JSON Schema

to_named_json_schema(schema_name, obj, ref_location='#/components/schemas/')#

Produces a Serializable from a Validator, Predicate or PredicateAsync that conforms to a valid JSON Schema. The root dict’s only key is the schema name, which has the full JSON Schema as its value.

Parameters:
  • schema_name (str) – mainly required when recursive objects need to related to themselves

  • obj (Union[Validator[Any], Predicate[Any], PredicateAsync[Any]]) – the Validator being described

  • ref_location (str, default: '#/components/schemas/') – where the schema name will be found if/when making a recursive reference to it

Return type:

Dict[str, Union[None, int, str, bool, float, List[Union[None, int, str, bool, float, List[Serializable], Tuple[Serializable, ...], Dict[str, Serializable]]], Tuple[Union[None, int, str, bool, float, List[Serializable], Tuple[Serializable, ...], Dict[str, Serializable]], ...], Dict[str, Union[None, int, str, bool, float, List[Serializable], Tuple[Serializable, ...], Dict[str, Serializable]]]]]

Returns:

a Serializable compatible with JSON Schema

to_serializable_errs(invalid, next_level=None)#

Convert an Invalid instance to human readable (English) Serializable representation of Invalid.

It can serve as an example of how to build similar functions to convert Invalid instances to other formats, other languages, etc.

Parameters:
  • invalid (Invalid) – The error you’d like to represent

  • next_level (Optional[Callable[[Invalid], Union[None, int, str, bool, float, List[Union[None, int, str, bool, float, List[Serializable], Tuple[Serializable, ...], Dict[str, Serializable]]], Tuple[Union[None, int, str, bool, float, List[Serializable], Tuple[Serializable, ...], Dict[str, Serializable]], ...], Dict[str, Union[None, int, str, bool, float, List[Serializable], Tuple[Serializable, ...], Dict[str, Serializable]]]]]], default: None) – If supplied, this callable will handle any calls for container ErrType``s such as ``ContainerErr, KeyErr, and so on

Return type:

Union[None, int, str, bool, float, List[Union[None, int, str, bool, float, List[Serializable], Tuple[Serializable, ...], Dict[str, Serializable]]], Tuple[Union[None, int, str, bool, float, List[Serializable], Tuple[Serializable, ...], Dict[str, Serializable]], ...], Dict[str, Union[None, int, str, bool, float, List[Serializable], Tuple[Serializable, ...], Dict[str, Serializable]]]]

Returns:

an error message appropriate for serializing in JSON or YAML. Because this function can handle any kind of Invalid, any type narrowing desired beyond Serializable needs to be done outside of this function.

Serializable#

alias of Union[None, int, str, bool, float, List[Serializable], Tuple[Serializable, …], Dict[str, Serializable]]