23
23
.. code-block:: bash
24
24
25
25
$ make performance
26
- fast_compiled valid ==> 0.026877017982769758
27
- fast_compiled invalid ==> 0.0015628149849362671
28
- fast_file valid ==> 0.025493122986517847
29
- fast_file invalid ==> 0.0012430319911800325
30
- fast_not_compiled valid ==> 4.790547857992351
31
- fast_not_compiled invalid ==> 1.2642899919883348
32
- jsonschema valid ==> 5.036152001994196
33
- jsonschema invalid ==> 1.1929481109953485
34
- jsonspec valid ==> 7.196442283981014
35
- jsonspec invalid ==> 1.7245555499684997
36
- validictory valid ==> 0.36818933801259845
37
- validictory invalid ==> 0.022672351042274386
38
-
39
- This library follows and implements `JSON schema draft-04 <http://json-schema.org>`_. Sometimes
40
- it's not perfectly clear so I recommend also check out this `understaning json schema
41
- <https://spacetelescope.github.io/understanding-json-schema>`_.
26
+ fast_compiled valid ==> 0.030474655970465392
27
+ fast_compiled invalid ==> 0.0017561429995112121
28
+ fast_file valid ==> 0.028758891974575818
29
+ fast_file invalid ==> 0.0017655809642747045
30
+ fast_not_compiled valid ==> 4.597834145999514
31
+ fast_not_compiled invalid ==> 1.139162228035275
32
+ jsonschema valid ==> 5.014410221017897
33
+ jsonschema invalid ==> 1.1362981660058722
34
+ jsonspec valid ==> 8.1144932230236
35
+ jsonspec invalid ==> 2.0143173419637606
36
+ validictory valid ==> 0.4084212710149586
37
+ validictory invalid ==> 0.026061681972350925
38
+
39
+ This library follows and implements `JSON schema draft-04, draft-06 and draft-07
40
+ <http://json-schema.org>`_. Sometimes it's not perfectly clear so I recommend also
41
+ check out this `understaning json schema <https://spacetelescope.github.io/understanding-json-schema>`_.
42
42
43
43
Note that there are some differences compared to JSON schema standard:
44
44
45
45
* Regular expressions are full Python ones, not only what JSON schema allows. It's easier
46
46
to allow everything and also it's faster to compile without limits. So keep in mind that when
47
- you will use more advanced regular expression, it may not work with other library.
47
+ you will use more advanced regular expression, it may not work with other library or in
48
+ other language.
48
49
* JSON schema says you can use keyword ``default`` for providing default values. This implementation
49
50
uses that and always returns transformed input data.
50
51
51
52
Support only for Python 3.3 and higher.
52
53
"""
53
54
54
- from os .path import exists
55
-
56
- from .exceptions import JsonSchemaException
57
55
from .draft04 import CodeGeneratorDraft04
58
56
from .draft06 import CodeGeneratorDraft06
59
57
from .draft07 import CodeGeneratorDraft07
58
+ from .exceptions import JsonSchemaException
60
59
from .ref_resolver import RefResolver
61
60
from .version import VERSION
62
61
63
62
__all__ = ('VERSION' , 'JsonSchemaException' , 'compile' , 'compile_to_code' )
64
63
65
64
66
65
# pylint: disable=redefined-builtin,dangerous-default-value,exec-used
67
- def compile (definition , version = 7 , handlers = {}):
66
+ def compile (definition , handlers = {}):
68
67
"""
69
68
Generates validation function for validating JSON schema by ``definition``. Example:
70
69
@@ -89,22 +88,31 @@ def compile(definition, version=7, handlers={}):
89
88
data = validate({})
90
89
assert data == {'a': 42}
91
90
92
- Args:
93
- definition (dict): Json schema definition
94
- handlers (dict): A mapping from URI schemes to functions
95
- that should be used to retrieve them.
91
+ Supported implementations are draft-04, draft-06 and draft-07. Which version
92
+ should be used is determined by `$draft` in your ``definition``. When not
93
+ specified, the latest implementation is used (draft-07).
94
+
95
+ .. code-block:: python
96
+
97
+ validate = fastjsonschema.compile({
98
+ '$schema': 'http://json-schema.org/draft-04/schema',
99
+ 'type': 'number',
100
+ })
101
+
102
+ You can pass mapping from URI to function that should be used to retrieve
103
+ remote schemes used in your ``definition`` in parameter ``handlers``.
96
104
97
105
Exception :any:`JsonSchemaException` is thrown when validation fails.
98
106
"""
99
- resolver , code_generator = _factory (definition , version , handlers )
107
+ resolver , code_generator = _factory (definition , handlers )
100
108
global_state = code_generator .global_state
101
109
# Do not pass local state so it can recursively call itself.
102
110
exec (code_generator .func_code , global_state )
103
111
return global_state [resolver .get_scope_name ()]
104
112
105
113
106
114
# pylint: disable=dangerous-default-value
107
- def compile_to_code (definition , version = 7 , handlers = {}):
115
+ def compile_to_code (definition , handlers = {}):
108
116
"""
109
117
Generates validation function for validating JSON schema by ``definition``
110
118
and returns compiled code. Example:
@@ -126,25 +134,26 @@ def compile_to_code(definition, version=7, handlers={}):
126
134
127
135
Exception :any:`JsonSchemaException` is thrown when validation fails.
128
136
"""
129
- _ , code_generator = _factory (definition , version , handlers )
137
+ _ , code_generator = _factory (definition , handlers )
130
138
return (
131
139
'VERSION = "' + VERSION + '"\n ' +
132
140
code_generator .global_state_code + '\n ' +
133
141
code_generator .func_code
134
142
)
135
143
136
144
137
- def _factory (definition , version , handlers ):
145
+ def _factory (definition , handlers ):
138
146
resolver = RefResolver .from_schema (definition , handlers = handlers )
139
- code_generator = _get_code_generator_class (version )(definition , resolver = resolver )
147
+ code_generator = _get_code_generator_class (definition )(definition , resolver = resolver )
140
148
return resolver , code_generator
141
149
142
150
143
- def _get_code_generator_class (version ):
144
- if version == 4 :
145
- return CodeGeneratorDraft04
146
- if version == 6 :
147
- return CodeGeneratorDraft06
148
- if version == 7 :
149
- return CodeGeneratorDraft07
150
- raise JsonSchemaException ('Unsupported JSON schema version. Supported are 4, 6 and 7.' )
151
+ def _get_code_generator_class (schema ):
152
+ # Schema in from draft-06 can be just the boolean value.
153
+ if isinstance (schema , dict ):
154
+ schema_version = schema .get ('$schema' , '' )
155
+ if 'draft-04' in schema_version :
156
+ return CodeGeneratorDraft04
157
+ if 'draft-06' in schema_version :
158
+ return CodeGeneratorDraft06
159
+ return CodeGeneratorDraft07
0 commit comments