42
42
.. code-block:: bash
43
43
44
44
$ make performance
45
- fast_compiled valid ==> 0.0464646
46
- fast_compiled invalid ==> 0.0030227
47
- fast_file valid ==> 0.0461219
48
- fast_file invalid ==> 0.0030608
49
- fast_not_compiled valid ==> 11.4627202
50
- fast_not_compiled invalid ==> 2.5726230
51
- jsonschema valid ==> 7.5844927
52
- jsonschema invalid ==> 1.9204665
53
- jsonschema_compiled valid ==> 0.6938364
54
- jsonschema_compiled invalid ==> 0.0359244
55
- jsonspec valid ==> 9.0715843
56
- jsonspec invalid ==> 2.1650488
57
- validictory valid ==> 0.4874793
58
- validictory invalid ==> 0.0232244
45
+ fast_compiled valid ==> 0.0993900
46
+ fast_compiled invalid ==> 0.0041089
47
+ fast_compiled_without_exc valid ==> 0.0465258
48
+ fast_compiled_without_exc invalid ==> 0.0023688
49
+ fast_file valid ==> 0.0989483
50
+ fast_file invalid ==> 0.0041104
51
+ fast_not_compiled valid ==> 11.9572681
52
+ fast_not_compiled invalid ==> 2.9512092
53
+ jsonschema valid ==> 5.2233240
54
+ jsonschema invalid ==> 1.3227916
55
+ jsonschema_compiled valid ==> 0.4447982
56
+ jsonschema_compiled invalid ==> 0.0231333
57
+ jsonspec valid ==> 4.1450569
58
+ jsonspec invalid ==> 1.0485777
59
+ validictory valid ==> 0.2730411
60
+ validictory invalid ==> 0.0183669
59
61
60
62
This library follows and implements `JSON schema draft-04, draft-06, and draft-07
61
63
<http://json-schema.org>`_. Sometimes it's not perfectly clear, so I recommend also
123
125
)
124
126
125
127
126
- def validate (definition , data , handlers = {}, formats = {}, use_default = True , use_formats = True ):
128
+ def validate (definition , data , handlers = {}, formats = {}, use_default = True , use_formats = True , detailed_exceptions = True ):
127
129
"""
128
130
Validation function for lazy programmers or for use cases when you need
129
131
to call validation only once, so you do not have to compile it first.
@@ -139,12 +141,12 @@ def validate(definition, data, handlers={}, formats={}, use_default=True, use_fo
139
141
140
142
Preferred is to use :any:`compile` function.
141
143
"""
142
- return compile (definition , handlers , formats , use_default , use_formats )(data )
144
+ return compile (definition , handlers , formats , use_default , use_formats , detailed_exceptions )(data )
143
145
144
146
145
147
#TODO: Change use_default to False when upgrading to version 3.
146
148
# pylint: disable=redefined-builtin,dangerous-default-value,exec-used
147
- def compile (definition , handlers = {}, formats = {}, use_default = True , use_formats = True ):
149
+ def compile (definition , handlers = {}, formats = {}, use_default = True , use_formats = True , detailed_exceptions = True ):
148
150
"""
149
151
Generates validation function for validating JSON schema passed in ``definition``.
150
152
Example:
@@ -200,13 +202,16 @@ def compile(definition, handlers={}, formats={}, use_default=True, use_formats=T
200
202
off by passing `use_formats=False`. When disabled, custom formats are
201
203
disabled as well. (Added in 2.19.0.)
202
204
205
+ If you don't need detailed exceptions, you can turn the details off and gain
206
+ additional performance by passing `detailed_exceptions=False`.
207
+
203
208
Exception :any:`JsonSchemaDefinitionException` is raised when generating the
204
209
code fails (bad definition).
205
210
206
211
Exception :any:`JsonSchemaValueException` is raised from generated function when
207
212
validation fails (data do not follow the definition).
208
213
"""
209
- resolver , code_generator = _factory (definition , handlers , formats , use_default , use_formats )
214
+ resolver , code_generator = _factory (definition , handlers , formats , use_default , use_formats , detailed_exceptions )
210
215
global_state = code_generator .global_state
211
216
# Do not pass local state so it can recursively call itself.
212
217
exec (code_generator .func_code , global_state )
@@ -217,7 +222,7 @@ def compile(definition, handlers={}, formats={}, use_default=True, use_formats=T
217
222
218
223
219
224
# pylint: disable=dangerous-default-value
220
- def compile_to_code (definition , handlers = {}, formats = {}, use_default = True , use_formats = True ):
225
+ def compile_to_code (definition , handlers = {}, formats = {}, use_default = True , use_formats = True , detailed_exceptions = True ):
221
226
"""
222
227
Generates validation code for validating JSON schema passed in ``definition``.
223
228
Example:
@@ -240,22 +245,23 @@ def compile_to_code(definition, handlers={}, formats={}, use_default=True, use_f
240
245
Exception :any:`JsonSchemaDefinitionException` is raised when generating the
241
246
code fails (bad definition).
242
247
"""
243
- _ , code_generator = _factory (definition , handlers , formats , use_default , use_formats )
248
+ _ , code_generator = _factory (definition , handlers , formats , use_default , use_formats , detailed_exceptions )
244
249
return (
245
250
'VERSION = "' + VERSION + '"\n ' +
246
251
code_generator .global_state_code + '\n ' +
247
252
code_generator .func_code
248
253
)
249
254
250
255
251
- def _factory (definition , handlers , formats = {}, use_default = True , use_formats = True ):
256
+ def _factory (definition , handlers , formats = {}, use_default = True , use_formats = True , detailed_exceptions = True ):
252
257
resolver = RefResolver .from_schema (definition , handlers = handlers , store = {})
253
258
code_generator = _get_code_generator_class (definition )(
254
259
definition ,
255
260
resolver = resolver ,
256
261
formats = formats ,
257
262
use_default = use_default ,
258
263
use_formats = use_formats ,
264
+ detailed_exceptions = detailed_exceptions ,
259
265
)
260
266
return resolver , code_generator
261
267
0 commit comments