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