|
55 | 55 | from .draft04 import CodeGeneratorDraft04
|
56 | 56 | from .draft06 import CodeGeneratorDraft06
|
57 | 57 | from .draft07 import CodeGeneratorDraft07
|
58 |
| -from .exceptions import JsonSchemaException |
| 58 | +from .exceptions import JsonSchemaException, JsonSchemaDefinitionException |
59 | 59 | from .ref_resolver import RefResolver
|
60 | 60 | from .version import VERSION
|
61 | 61 |
|
62 |
| -__all__ = ('VERSION', 'JsonSchemaException', 'compile', 'compile_to_code') |
| 62 | +__all__ = ('VERSION', 'JsonSchemaException', 'validate', 'compile', 'compile_to_code') |
| 63 | + |
| 64 | + |
| 65 | +def validate(definition, data): |
| 66 | + """ |
| 67 | + Validation function for lazy programmers or for use cases, when you need |
| 68 | + to call validation only once, so you do not have to compile it first. |
| 69 | + Use it only when you do not care about performance (even thought it will |
| 70 | + be still faster than alternative implementations). |
| 71 | +
|
| 72 | + .. code-block:: python |
| 73 | +
|
| 74 | + import fastjsonschema |
| 75 | +
|
| 76 | + validate({'type': 'string'}, 'hello') |
| 77 | + # same as: compile({'type': 'string'})('hello') |
| 78 | +
|
| 79 | + Preffered is to use :any:`compile` function. |
| 80 | + """ |
| 81 | + return compile(definition)(data) |
63 | 82 |
|
64 | 83 |
|
65 | 84 | # pylint: disable=redefined-builtin,dangerous-default-value,exec-used
|
@@ -103,8 +122,11 @@ def compile(definition, handlers={}):
|
103 | 122 | You can pass mapping from URI to function that should be used to retrieve
|
104 | 123 | remote schemes used in your ``definition`` in parameter ``handlers``.
|
105 | 124 |
|
106 |
| - Exception :any:`JsonSchemaException` is thrown when generation code fail |
107 |
| - (wrong definition) or validation fails (data does not follow definition). |
| 125 | + Exception :any:`JsonSchemaDefinitionException` is raised when generating the |
| 126 | + code fails (bad definition). |
| 127 | +
|
| 128 | + Exception :any:`JsonSchemaException` is raised from generated funtion when |
| 129 | + validation fails (data do not follow the definition). |
108 | 130 | """
|
109 | 131 | resolver, code_generator = _factory(definition, handlers)
|
110 | 132 | global_state = code_generator.global_state
|
@@ -133,6 +155,9 @@ def compile_to_code(definition, handlers={}):
|
133 | 155 |
|
134 | 156 | echo "{'type': 'string'}" | python3 -m fastjsonschema > your_file.py
|
135 | 157 | python3 -m fastjsonschema "{'type': 'string'}" > your_file.py
|
| 158 | +
|
| 159 | + Exception :any:`JsonSchemaDefinitionException` is raised when generating the |
| 160 | + code fails (bad definition). |
136 | 161 | """
|
137 | 162 | _, code_generator = _factory(definition, handlers)
|
138 | 163 | return (
|
|
0 commit comments