Type Validation#

oblate.validate_types(types, values, *, ignore_extra=False, ignore_missing=False)#

Validates the types of given values.

The first parameter is the mapping with values representing the type for given key. Second parameter is mapping for the values for each key in the first parameter.

For more information on supported types and limitations of this function, see the Type Validation section.

Example:

import typing
import oblate

types = {
    'name': str,
    'id': typing.Union[int, str],
}

oblate.validate_types(types, {'name': 'John', 'id': 2})  # no error

Example error:

try:
    oblate.validate_types(types, {'name': 12})
except oblate.TypeValidationError as err:
    print(err.errors)
    # > {'name': ['Must be of type str'], 'id': ['This key is missing.']}
Parameters:
  • types (Mapping[str, type]) – The types to validate from.

  • values (Mapping[str, Any]) – The values to be validated.

  • ignore_extra (bool) – Whether to ignore extra keys in values mapping. Defaults to False.

  • ignore_missing (bool) – Whether to ignore missing keys in values mapping.

Raises:

TypeValidationError – The type validation failed.