jsonschema.exceptions#

Validation errors, and some surrounding helpers.

class jsonschema.exceptions.ErrorTree(errors: Iterable[ValidationError] = ())[source]#

ErrorTrees make it easier to check which validations failed.

property total_errors#

The total number of errors in the entire tree, including children.

exception jsonschema.exceptions.FormatError(message, cause=None)[source]#

Validating a format failed.

exception jsonschema.exceptions.SchemaError(message: str, validator=<unset>, path=(), cause=None, context=(), validator_value=<unset>, instance=<unset>, schema=<unset>, schema_path=(), parent=None, type_checker=<unset>)[source]#

A schema was invalid under its corresponding metaschema.

exception jsonschema.exceptions.UndefinedTypeCheck(type)[source]#

A type checker was asked to check a type it did not have registered.

exception jsonschema.exceptions.UnknownType(type, instance, schema)[source]#

A validator was asked to validate an instance against an unknown type.

exception jsonschema.exceptions.ValidationError(message: str, validator=<unset>, path=(), cause=None, context=(), validator_value=<unset>, instance=<unset>, schema=<unset>, schema_path=(), parent=None, type_checker=<unset>)[source]#

An instance was invalid under a provided schema.

jsonschema.exceptions.best_match(errors, key=<function by_relevance.<locals>.relevance>)[source]#

Try to find an error that appears to be the best match among given errors.

In general, errors that are higher up in the instance (i.e. for which ValidationError.path is shorter) are considered better matches, since they indicate “more” is wrong with the instance.

If the resulting match is either oneOf or anyOf, the opposite assumption is made – i.e. the deepest error is picked, since these keywords only need to match once, and any other errors may not be relevant.

Parameters:
  • errors (collections.abc.Iterable) – the errors to select from. Do not provide a mixture of errors from different validation attempts (i.e. from different instances or schemas), since it won’t produce sensical output.

  • key (collections.abc.Callable) – the key to use when sorting errors. See relevance and transitively by_relevance for more details (the default is to sort with the defaults of that function). Changing the default is only useful if you want to change the function that rates errors but still want the error context descent done by this function.

Returns:

the best matching error, or None if the iterable was empty

Note

This function is a heuristic. Its return value may change for a given set of inputs from version to version if better heuristics are added.

jsonschema.exceptions.by_relevance(weak=frozenset({'anyOf', 'oneOf'}), strong=frozenset({}))[source]#

Create a key function that can be used to sort errors by relevance.

Parameters:
  • weak (set) – a collection of validation keywords to consider to be “weak”. If there are two errors at the same level of the instance and one is in the set of weak validation keywords, the other error will take priority. By default, anyOf and oneOf are considered weak keywords and will be superseded by other same-level validation errors.

  • strong (set) – a collection of validation keywords to consider to be “strong”

jsonschema.exceptions.relevance(error)#

A key function (e.g. to use with sorted) which sorts errors by relevance.

Example:

sorted(validator.iter_errors(12), key=jsonschema.exceptions.relevance)