1
1
import re
2
2
3
+ from .exceptions import JsonSchemaDefinitionException
3
4
from .generator import CodeGenerator , enforce_list
4
5
5
6
@@ -65,7 +66,10 @@ def generate_type(self):
65
66
{'type': ['string', 'number']}
66
67
"""
67
68
types = enforce_list (self ._definition ['type' ])
68
- python_types = ', ' .join (JSON_TYPE_TO_PYTHON_TYPE .get (t ) for t in types )
69
+ try :
70
+ python_types = ', ' .join (JSON_TYPE_TO_PYTHON_TYPE [t ] for t in types )
71
+ except KeyError as exc :
72
+ raise JsonSchemaDefinitionException ('Unknown type: {}' .format (exc ))
69
73
70
74
extra = ''
71
75
if ('number' in types or 'integer' in types ) and 'boolean' not in types :
@@ -84,6 +88,8 @@ def generate_enum(self):
84
88
'enum': ['a', 'b'],
85
89
}
86
90
"""
91
+ if not isinstance (self ._definition ['enum' ], (list , tuple )):
92
+ raise JsonSchemaDefinitionException ('enum must be an array' )
87
93
with self .l ('if {variable} not in {enum}:' ):
88
94
self .l ('raise JsonSchemaException("{name} must be one of {enum}")' )
89
95
@@ -192,20 +198,25 @@ def generate_not(self):
192
198
def generate_min_length (self ):
193
199
with self .l ('if isinstance({variable}, str):' ):
194
200
self .create_variable_with_length ()
201
+ if not isinstance (self ._definition ['minLength' ], int ):
202
+ raise JsonSchemaDefinitionException ('minLength must be a number' )
195
203
with self .l ('if {variable}_len < {minLength}:' ):
196
204
self .l ('raise JsonSchemaException("{name} must be longer than or equal to {minLength} characters")' )
197
205
198
206
def generate_max_length (self ):
199
207
with self .l ('if isinstance({variable}, str):' ):
200
208
self .create_variable_with_length ()
209
+ if not isinstance (self ._definition ['maxLength' ], int ):
210
+ raise JsonSchemaDefinitionException ('maxLength must be a number' )
201
211
with self .l ('if {variable}_len > {maxLength}:' ):
202
212
self .l ('raise JsonSchemaException("{name} must be shorter than or equal to {maxLength} characters")' )
203
213
204
214
def generate_pattern (self ):
205
215
with self .l ('if isinstance({variable}, str):' ):
206
- self ._compile_regexps ['{}' .format (self ._definition ['pattern' ])] = re .compile (self ._definition ['pattern' ])
207
- with self .l ('if not REGEX_PATTERNS["{}"].search({variable}):' , self ._definition ['pattern' ]):
208
- self .l ('raise JsonSchemaException("{name} must match pattern {pattern}")' )
216
+ safe_pattern = self ._definition ['pattern' ].replace ('"' , '\\ "' )
217
+ self ._compile_regexps [self ._definition ['pattern' ]] = re .compile (self ._definition ['pattern' ])
218
+ with self .l ('if not REGEX_PATTERNS["{}"].search({variable}):' , safe_pattern ):
219
+ self .l ('raise JsonSchemaException("{name} must match pattern {}")' , safe_pattern )
209
220
210
221
def generate_format (self ):
211
222
"""
@@ -240,6 +251,8 @@ def _generate_format(self, format_name, regexp_name, regexp):
240
251
241
252
def generate_minimum (self ):
242
253
with self .l ('if isinstance({variable}, (int, float)):' ):
254
+ if not isinstance (self ._definition ['minimum' ], (int , float )):
255
+ raise JsonSchemaDefinitionException ('minimum must be a number' )
243
256
if self ._definition .get ('exclusiveMinimum' , False ):
244
257
with self .l ('if {variable} <= {minimum}:' ):
245
258
self .l ('raise JsonSchemaException("{name} must be bigger than {minimum}")' )
@@ -249,6 +262,8 @@ def generate_minimum(self):
249
262
250
263
def generate_maximum (self ):
251
264
with self .l ('if isinstance({variable}, (int, float)):' ):
265
+ if not isinstance (self ._definition ['maximum' ], (int , float )):
266
+ raise JsonSchemaDefinitionException ('maximum must be a number' )
252
267
if self ._definition .get ('exclusiveMaximum' , False ):
253
268
with self .l ('if {variable} >= {maximum}:' ):
254
269
self .l ('raise JsonSchemaException("{name} must be smaller than {maximum}")' )
@@ -258,20 +273,26 @@ def generate_maximum(self):
258
273
259
274
def generate_multiple_of (self ):
260
275
with self .l ('if isinstance({variable}, (int, float)):' ):
276
+ if not isinstance (self ._definition ['multipleOf' ], (int , float )):
277
+ raise JsonSchemaDefinitionException ('multipleOf must be a number' )
261
278
self .l ('quotient = {variable} / {multipleOf}' )
262
279
with self .l ('if int(quotient) != quotient:' ):
263
280
self .l ('raise JsonSchemaException("{name} must be multiple of {multipleOf}")' )
264
281
265
282
def generate_min_items (self ):
266
283
self .create_variable_is_list ()
267
284
with self .l ('if {variable}_is_list:' ):
285
+ if not isinstance (self ._definition ['minItems' ], int ):
286
+ raise JsonSchemaDefinitionException ('minItems must be a number' )
268
287
self .create_variable_with_length ()
269
288
with self .l ('if {variable}_len < {minItems}:' ):
270
289
self .l ('raise JsonSchemaException("{name} must contain at least {minItems} items")' )
271
290
272
291
def generate_max_items (self ):
273
292
self .create_variable_is_list ()
274
293
with self .l ('if {variable}_is_list:' ):
294
+ if not isinstance (self ._definition ['maxItems' ], int ):
295
+ raise JsonSchemaDefinitionException ('maxItems must be a number' )
275
296
self .create_variable_with_length ()
276
297
with self .l ('if {variable}_len > {maxItems}:' ):
277
298
self .l ('raise JsonSchemaException("{name} must contain less than or equal to {maxItems} items")' )
@@ -357,20 +378,26 @@ def generate_items(self):
357
378
def generate_min_properties (self ):
358
379
self .create_variable_is_dict ()
359
380
with self .l ('if {variable}_is_dict:' ):
381
+ if not isinstance (self ._definition ['minProperties' ], int ):
382
+ raise JsonSchemaDefinitionException ('minProperties must be a number' )
360
383
self .create_variable_with_length ()
361
384
with self .l ('if {variable}_len < {minProperties}:' ):
362
385
self .l ('raise JsonSchemaException("{name} must contain at least {minProperties} properties")' )
363
386
364
387
def generate_max_properties (self ):
365
388
self .create_variable_is_dict ()
366
389
with self .l ('if {variable}_is_dict:' ):
390
+ if not isinstance (self ._definition ['maxProperties' ], int ):
391
+ raise JsonSchemaDefinitionException ('maxProperties must be a number' )
367
392
self .create_variable_with_length ()
368
393
with self .l ('if {variable}_len > {maxProperties}:' ):
369
394
self .l ('raise JsonSchemaException("{name} must contain less than or equal to {maxProperties} properties")' )
370
395
371
396
def generate_required (self ):
372
397
self .create_variable_is_dict ()
373
398
with self .l ('if {variable}_is_dict:' ):
399
+ if not isinstance (self ._definition ['required' ], (list , tuple )):
400
+ raise JsonSchemaDefinitionException ('required must be an array' )
374
401
self .create_variable_with_length ()
375
402
with self .l ('if not all(prop in {variable} for prop in {required}):' ):
376
403
self .l ('raise JsonSchemaException("{name} must contain {required} properties")' )
0 commit comments