110110from .draft04 import CodeGeneratorDraft04
111111from .draft06 import CodeGeneratorDraft06
112112from .draft07 import CodeGeneratorDraft07
113- from .exceptions import JsonSchemaException , JsonSchemaValueException , JsonSchemaDefinitionException
113+ from .exceptions import JsonSchemaException , JsonSchemaValueException , JsonSchemaValuesException , JsonSchemaDefinitionException
114114from .ref_resolver import RefResolver
115115from .version import VERSION
116116
117117__all__ = (
118118 'VERSION' ,
119119 'JsonSchemaException' ,
120120 'JsonSchemaValueException' ,
121+ 'JsonSchemaValuesException' ,
121122 'JsonSchemaDefinitionException' ,
122123 'validate' ,
123124 'compile' ,
124125 'compile_to_code' ,
125126)
126127
127128
128- def validate (definition , data , handlers = {}, formats = {}, use_default = True , use_formats = True , detailed_exceptions = True ):
129+ def validate (definition , data , handlers = {}, formats = {}, use_default = True , use_formats = True , detailed_exceptions = True , fast_fail = True ):
129130 """
130131 Validation function for lazy programmers or for use cases when you need
131132 to call validation only once, so you do not have to compile it first.
@@ -141,12 +142,12 @@ def validate(definition, data, handlers={}, formats={}, use_default=True, use_fo
141142
142143 Preferred is to use :any:`compile` function.
143144 """
144- return compile (definition , handlers , formats , use_default , use_formats , detailed_exceptions )(data )
145+ return compile (definition , handlers , formats , use_default , use_formats , detailed_exceptions , fast_fail )(data )
145146
146147
147148#TODO: Change use_default to False when upgrading to version 3.
148149# pylint: disable=redefined-builtin,dangerous-default-value,exec-used
149- def compile (definition , handlers = {}, formats = {}, use_default = True , use_formats = True , detailed_exceptions = True ):
150+ def compile (definition , handlers = {}, formats = {}, use_default = True , use_formats = True , detailed_exceptions = True , fast_fail = True ):
150151 """
151152 Generates validation function for validating JSON schema passed in ``definition``.
152153 Example:
@@ -205,13 +206,20 @@ def compile(definition, handlers={}, formats={}, use_default=True, use_formats=T
205206 If you don't need detailed exceptions, you can turn the details off and gain
206207 additional performance by passing `detailed_exceptions=False`.
207208
209+ By default, the execution stops with the first validation error. If you need
210+ to collect all the errors, turn this off by passing `fast_fail=False`.
211+
208212 Exception :any:`JsonSchemaDefinitionException` is raised when generating the
209213 code fails (bad definition).
210214
211215 Exception :any:`JsonSchemaValueException` is raised from generated function when
212216 validation fails (data do not follow the definition).
217+
218+ Exception :any:`JsonSchemaValuesException` is raised from generated function when
219+ validation fails (data do not follow the definition) contatining all the errors
220+ (when fast_fail is set to `False`).
213221 """
214- resolver , code_generator = _factory (definition , handlers , formats , use_default , use_formats , detailed_exceptions )
222+ resolver , code_generator = _factory (definition , handlers , formats , use_default , use_formats , detailed_exceptions , fast_fail )
215223 global_state = code_generator .global_state
216224 # Do not pass local state so it can recursively call itself.
217225 exec (code_generator .func_code , global_state )
@@ -222,7 +230,7 @@ def compile(definition, handlers={}, formats={}, use_default=True, use_formats=T
222230
223231
224232# pylint: disable=dangerous-default-value
225- def compile_to_code (definition , handlers = {}, formats = {}, use_default = True , use_formats = True , detailed_exceptions = True ):
233+ def compile_to_code (definition , handlers = {}, formats = {}, use_default = True , use_formats = True , detailed_exceptions = True , fast_fail = True ):
226234 """
227235 Generates validation code for validating JSON schema passed in ``definition``.
228236 Example:
@@ -245,15 +253,15 @@ def compile_to_code(definition, handlers={}, formats={}, use_default=True, use_f
245253 Exception :any:`JsonSchemaDefinitionException` is raised when generating the
246254 code fails (bad definition).
247255 """
248- _ , code_generator = _factory (definition , handlers , formats , use_default , use_formats , detailed_exceptions )
256+ _ , code_generator = _factory (definition , handlers , formats , use_default , use_formats , detailed_exceptions , fast_fail )
249257 return (
250258 'VERSION = "' + VERSION + '"\n ' +
251259 code_generator .global_state_code + '\n ' +
252260 code_generator .func_code
253261 )
254262
255263
256- def _factory (definition , handlers , formats = {}, use_default = True , use_formats = True , detailed_exceptions = True ):
264+ def _factory (definition , handlers , formats = {}, use_default = True , use_formats = True , detailed_exceptions = True , fast_fail = True ):
257265 resolver = RefResolver .from_schema (definition , handlers = handlers , store = {})
258266 code_generator = _get_code_generator_class (definition )(
259267 definition ,
@@ -262,6 +270,7 @@ def _factory(definition, handlers, formats={}, use_default=True, use_formats=Tru
262270 use_default = use_default ,
263271 use_formats = use_formats ,
264272 detailed_exceptions = detailed_exceptions ,
273+ fast_fail = fast_fail ,
265274 )
266275 return resolver , code_generator
267276
0 commit comments