Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/fastjsonschema/__init__.py: 40%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

35 statements  

1# ___ 

2# \./ DANGER: This project implements some code generation 

3# .--.O.--. techniques involving string concatenation. 

4# \/ \/ If you look at it, you might die. 

5# 

6 

7r""" 

8Installation 

9************ 

10 

11.. code-block:: bash 

12 

13 pip install fastjsonschema 

14 

15Support only for Python 3.3 and higher. 

16 

17About 

18***** 

19 

20``fastjsonschema`` implements validation of JSON documents by JSON schema. 

21The library implements JSON schema drafts 04, 06, and 07. The main purpose is 

22to have a really fast implementation. See some numbers: 

23 

24 * Probably the most popular, ``jsonschema``, can take up to 5 seconds for valid 

25 inputs and 1.2 seconds for invalid inputs. 

26 * Second most popular, ``json-spec``, is even worse with up to 7.2 and 1.7 seconds. 

27 * Last ``validictory``, now deprecated, is much better with 370 or 23 milliseconds, 

28 but it does not follow all standards, and it can be still slow for some purposes. 

29 

30With this library you can gain big improvements as ``fastjsonschema`` takes 

31only about 25 milliseconds for valid inputs and 2 milliseconds for invalid ones. 

32Pretty amazing, right? :-) 

33 

34Technically it works by generating the most stupid code on the fly, which is fast but 

35is hard to write by hand. The best efficiency is achieved when a validator is compiled 

36once and used many times, of course. It works similarly like regular expressions. But 

37you can also generate the code to a file, which is even slightly faster. 

38 

39You can run the performance benchmarks on your computer or server with the included 

40script: 

41 

42.. code-block:: bash 

43 

44 $ make performance 

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 

61 

62This library follows and implements `JSON schema draft-04, draft-06, and draft-07 

63<http://json-schema.org>`_. Sometimes it's not perfectly clear, so I recommend also 

64check out this `understanding JSON schema <https://spacetelescope.github.io/understanding-json-schema>`_. 

65 

66Note that there are some differences compared to JSON schema standard: 

67 

68 * Regular expressions are full Python ones, not only what JSON schema allows. It's easier 

69 to allow everything, and also it's faster to compile without limits. So keep in mind that when 

70 you will use a more advanced regular expression, it may not work with other libraries or in 

71 other languages. 

72 * Because Python matches new line for a dollar in regular expressions (``a$`` matches ``a`` and ``a\\n``), 

73 instead of ``$`` is used ``\Z`` and all dollars in your regular expression are changed to ``\\Z`` 

74 as well. When you want to use dollar as regular character, you have to escape it (``\$``). 

75 * JSON schema says you can use keyword ``default`` for providing default values. This implementation 

76 uses that and always returns transformed input data. 

77 

78Usage 

79***** 

80 

81.. code-block:: python 

82 

83 import fastjsonschema 

84 

85 point_schema = { 

86 "type": "object", 

87 "properties": { 

88 "x": { 

89 "type": "number", 

90 }, 

91 "y": { 

92 "type": "number", 

93 }, 

94 }, 

95 "required": ["x", "y"], 

96 "additionalProperties": False, 

97 } 

98 

99 point_validator = fastjsonschema.compile(point_schema) 

100 try: 

101 point_validator({"x": 1.0, "y": 2.0}) 

102 except fastjsonschema.JsonSchemaException as e: 

103 print(f"Data failed validation: {e}") 

104 

105API 

106*** 

107""" 

108from functools import partial, update_wrapper 

109 

110from .draft04 import CodeGeneratorDraft04 

111from .draft06 import CodeGeneratorDraft06 

112from .draft07 import CodeGeneratorDraft07 

113from .exceptions import JsonSchemaException, JsonSchemaValueException, JsonSchemaDefinitionException 

114from .ref_resolver import RefResolver 

115from .version import VERSION 

116 

117__all__ = ( 

118 'VERSION', 

119 'JsonSchemaException', 

120 'JsonSchemaValueException', 

121 'JsonSchemaDefinitionException', 

122 'validate', 

123 'compile', 

124 'compile_to_code', 

125) 

126 

127 

128def validate(definition, data, handlers={}, formats={}, use_default=True, use_formats=True, detailed_exceptions=True): 

129 """ 

130 Validation function for lazy programmers or for use cases when you need 

131 to call validation only once, so you do not have to compile it first. 

132 Use it only when you do not care about performance (even though it will 

133 be still faster than alternative implementations). 

134 

135 .. code-block:: python 

136 

137 import fastjsonschema 

138 

139 fastjsonschema.validate({'type': 'string'}, 'hello') 

140 # same as: compile({'type': 'string'})('hello') 

141 

142 Preferred is to use :any:`compile` function. 

143 """ 

144 return compile(definition, handlers, formats, use_default, use_formats, detailed_exceptions)(data) 

145 

146 

147#TODO: Change use_default to False when upgrading to version 3. 

148# pylint: disable=redefined-builtin,dangerous-default-value,exec-used 

149def compile(definition, handlers={}, formats={}, use_default=True, use_formats=True, detailed_exceptions=True): 

150 """ 

151 Generates validation function for validating JSON schema passed in ``definition``. 

152 Example: 

153 

154 .. code-block:: python 

155 

156 import fastjsonschema 

157 

158 validate = fastjsonschema.compile({'type': 'string'}) 

159 validate('hello') 

160 

161 This implementation supports keyword ``default`` (can be turned off 

162 by passing `use_default=False`): 

163 

164 .. code-block:: python 

165 

166 validate = fastjsonschema.compile({ 

167 'type': 'object', 

168 'properties': { 

169 'a': {'type': 'number', 'default': 42}, 

170 }, 

171 }) 

172 

173 data = validate({}) 

174 assert data == {'a': 42} 

175 

176 Supported implementations are draft-04, draft-06 and draft-07. Which version 

177 should be used is determined by `$draft` in your ``definition``. When not 

178 specified, the latest implementation is used (draft-07). 

179 

180 .. code-block:: python 

181 

182 validate = fastjsonschema.compile({ 

183 '$schema': 'http://json-schema.org/draft-04/schema', 

184 'type': 'number', 

185 }) 

186 

187 You can pass mapping from URI to function that should be used to retrieve 

188 remote schemes used in your ``definition`` in parameter ``handlers``. 

189 

190 Also, you can pass mapping for custom formats. Key is the name of your 

191 formatter and value can be regular expression, which will be compiled or 

192 callback returning `bool` (or you can raise your own exception). 

193 

194 .. code-block:: python 

195 

196 validate = fastjsonschema.compile(definition, formats={ 

197 'foo': r'foo|bar', 

198 'bar': lambda value: value in ('foo', 'bar'), 

199 }) 

200 

201 Note that formats are automatically used as assertions. It can be turned 

202 off by passing `use_formats=False`. When disabled, custom formats are 

203 disabled as well. (Added in 2.19.0.) 

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 

208 Exception :any:`JsonSchemaDefinitionException` is raised when generating the 

209 code fails (bad definition). 

210 

211 Exception :any:`JsonSchemaValueException` is raised from generated function when 

212 validation fails (data do not follow the definition). 

213 """ 

214 resolver, code_generator = _factory(definition, handlers, formats, use_default, use_formats, detailed_exceptions) 

215 global_state = code_generator.global_state 

216 # Do not pass local state so it can recursively call itself. 

217 exec(code_generator.func_code, global_state) 

218 func = global_state[resolver.get_scope_name()] 

219 if formats: 

220 return update_wrapper(partial(func, custom_formats=formats), func) 

221 return func 

222 

223 

224# pylint: disable=dangerous-default-value 

225def compile_to_code(definition, handlers={}, formats={}, use_default=True, use_formats=True, detailed_exceptions=True): 

226 """ 

227 Generates validation code for validating JSON schema passed in ``definition``. 

228 Example: 

229 

230 .. code-block:: python 

231 

232 import fastjsonschema 

233 

234 code = fastjsonschema.compile_to_code({'type': 'string'}) 

235 with open('your_file.py', 'w') as f: 

236 f.write(code) 

237 

238 You can also use it as a script: 

239 

240 .. code-block:: bash 

241 

242 echo "{'type': 'string'}" | python3 -m fastjsonschema > your_file.py 

243 python3 -m fastjsonschema "{'type': 'string'}" > your_file.py 

244 

245 Exception :any:`JsonSchemaDefinitionException` is raised when generating the 

246 code fails (bad definition). 

247 """ 

248 _, code_generator = _factory(definition, handlers, formats, use_default, use_formats, detailed_exceptions) 

249 return ( 

250 'VERSION = "' + VERSION + '"\n' + 

251 code_generator.global_state_code + '\n' + 

252 code_generator.func_code 

253 ) 

254 

255 

256def _factory(definition, handlers, formats={}, use_default=True, use_formats=True, detailed_exceptions=True): 

257 resolver = RefResolver.from_schema(definition, handlers=handlers, store={}) 

258 code_generator = _get_code_generator_class(definition)( 

259 definition, 

260 resolver=resolver, 

261 formats=formats, 

262 use_default=use_default, 

263 use_formats=use_formats, 

264 detailed_exceptions=detailed_exceptions, 

265 ) 

266 return resolver, code_generator 

267 

268 

269def _get_code_generator_class(schema): 

270 # Schema in from draft-06 can be just the boolean value. 

271 if isinstance(schema, dict): 

272 schema_version = schema.get('$schema', '') 

273 if 'draft-04' in schema_version: 

274 return CodeGeneratorDraft04 

275 if 'draft-06' in schema_version: 

276 return CodeGeneratorDraft06 

277 return CodeGeneratorDraft07