7
7
"""
8
8
This project was made to come up with fast JSON validations. Just let's see some numbers first:
9
9
10
- * Probalby most popular ``jsonschema`` can take in tests up to 5 seconds for valid inputs
10
+ * Probably most popular ``jsonschema`` can take up to 5 seconds for valid inputs
11
11
and 1.2 seconds for invalid inputs.
12
- * Secondly most popular ``json-spec`` is even worse with up to 7.2 and 1.7 seconds.
13
- * Lastly ``validictory`` is much better with 370 or 23 miliseconds , but it does not
12
+ * Second most popular ``json-spec`` is even worse with up to 7.2 and 1.7 seconds.
13
+ * Last ``validictory`` is much better with 370 or 23 milliseconds , but it does not
14
14
follow all standards and it can be still slow for some purposes.
15
15
16
16
That's why this project exists. It compiles definition into Python most stupid code
17
- which people would had hard time to write by themselfs because of not-written-rule DRY
18
- (don't repeat yourself). When you compile definition, then times are 25 miliseconds for
19
- valid inputs and less than 2 miliseconds for invalid inputs. Pretty amazing, right? :-)
17
+ which people would have hard time to write by themselves because of not-written-rule DRY
18
+ (don't repeat yourself). When you compile definition, then times are 25 milliseconds for
19
+ valid inputs and less than 2 milliseconds for invalid inputs. Pretty amazing, right? :-)
20
20
21
- You can try it for yourself with included script:
21
+ You can try it for yourself with an included script:
22
22
23
23
.. code-block:: bash
24
24
36
36
validictory valid ==> 0.4084212710149586
37
37
validictory invalid ==> 0.026061681972350925
38
38
39
- This library follows and implements `JSON schema draft-04, draft-06 and draft-07
39
+ This library follows and implements `JSON schema draft-04, draft-06, and draft-07
40
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>`_.
41
+ check out this `understanding 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 or in
48
- other language .
47
+ you will use a more advanced regular expression, it may not work with other library or in
48
+ other languages .
49
49
* JSON schema says you can use keyword ``default`` for providing default values. This implementation
50
50
uses that and always returns transformed input data.
51
51
65
65
# pylint: disable=redefined-builtin,dangerous-default-value,exec-used
66
66
def compile (definition , handlers = {}):
67
67
"""
68
- Generates validation function for validating JSON schema by ``definition``. Example:
68
+ Generates validation function for validating JSON schema passed in ``definition``.
69
+ Example:
69
70
70
71
.. code-block:: python
71
72
@@ -102,7 +103,8 @@ def compile(definition, handlers={}):
102
103
You can pass mapping from URI to function that should be used to retrieve
103
104
remote schemes used in your ``definition`` in parameter ``handlers``.
104
105
105
- Exception :any:`JsonSchemaException` is thrown when validation fails.
106
+ Exception :any:`JsonSchemaException` is thrown when generation code fail
107
+ (wrong definition) or validation fails (data does not follow definition).
106
108
"""
107
109
resolver , code_generator = _factory (definition , handlers )
108
110
global_state = code_generator .global_state
@@ -114,8 +116,8 @@ def compile(definition, handlers={}):
114
116
# pylint: disable=dangerous-default-value
115
117
def compile_to_code (definition , handlers = {}):
116
118
"""
117
- Generates validation function for validating JSON schema by ``definition``
118
- and returns compiled code. Example:
119
+ Generates validation code for validating JSON schema passed in ``definition``.
120
+ Example:
119
121
120
122
.. code-block:: python
121
123
@@ -131,8 +133,6 @@ def compile_to_code(definition, handlers={}):
131
133
132
134
echo "{'type': 'string'}" | python3 -m fastjsonschema > your_file.py
133
135
python3 -m fastjsonschema "{'type': 'string'}" > your_file.py
134
-
135
- Exception :any:`JsonSchemaException` is thrown when validation fails.
136
136
"""
137
137
_ , code_generator = _factory (definition , handlers )
138
138
return (
0 commit comments