Skip to content

Commit 8432be5

Browse files
committed
Added validate function
1 parent e88b686 commit 8432be5

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

CHANGELOG.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Fix overriding variables (in pattern properties, property names, unique items and contains)
44
* Fix string in const
55
* Improve security: not generating code from any definition
6+
* Added validate function for lazy programmers
67

78

89
=== 2.3 (2018-09-14) ===

fastjsonschema/__init__.py

+29-4
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,30 @@
5555
from .draft04 import CodeGeneratorDraft04
5656
from .draft06 import CodeGeneratorDraft06
5757
from .draft07 import CodeGeneratorDraft07
58-
from .exceptions import JsonSchemaException
58+
from .exceptions import JsonSchemaException, JsonSchemaDefinitionException
5959
from .ref_resolver import RefResolver
6060
from .version import VERSION
6161

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)
6382

6483

6584
# pylint: disable=redefined-builtin,dangerous-default-value,exec-used
@@ -103,8 +122,11 @@ def compile(definition, handlers={}):
103122
You can pass mapping from URI to function that should be used to retrieve
104123
remote schemes used in your ``definition`` in parameter ``handlers``.
105124
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).
108130
"""
109131
resolver, code_generator = _factory(definition, handlers)
110132
global_state = code_generator.global_state
@@ -133,6 +155,9 @@ def compile_to_code(definition, handlers={}):
133155
134156
echo "{'type': 'string'}" | python3 -m fastjsonschema > your_file.py
135157
python3 -m fastjsonschema "{'type': 'string'}" > your_file.py
158+
159+
Exception :any:`JsonSchemaDefinitionException` is raised when generating the
160+
code fails (bad definition).
136161
"""
137162
_, code_generator = _factory(definition, handlers)
138163
return (

0 commit comments

Comments
 (0)