__init__.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. from functools import reduce
  2. from typing import Any, Callable, Dict
  3. from . import formats
  4. from .error_reporting import detailed_errors, ValidationError
  5. from .extra_validations import EXTRA_VALIDATIONS
  6. from .fastjsonschema_exceptions import JsonSchemaException, JsonSchemaValueException
  7. from .fastjsonschema_validations import validate as _validate
  8. __all__ = [
  9. "validate",
  10. "FORMAT_FUNCTIONS",
  11. "EXTRA_VALIDATIONS",
  12. "ValidationError",
  13. "JsonSchemaException",
  14. "JsonSchemaValueException",
  15. ]
  16. FORMAT_FUNCTIONS: Dict[str, Callable[[str], bool]] = {
  17. fn.__name__.replace("_", "-"): fn
  18. for fn in formats.__dict__.values()
  19. if callable(fn) and not fn.__name__.startswith("_")
  20. }
  21. def validate(data: Any) -> bool:
  22. """Validate the given ``data`` object using JSON Schema
  23. This function raises ``ValidationError`` if ``data`` is invalid.
  24. """
  25. with detailed_errors():
  26. _validate(data, custom_formats=FORMAT_FUNCTIONS)
  27. reduce(lambda acc, fn: fn(acc), EXTRA_VALIDATIONS, data)
  28. return True