@@ -249,7 +249,72 @@ Features
249
249
" type" : " object"
250
250
}
251
251
252
- For more information, please see topic about validation in documentation.
252
+ You can also validate scalars, when needed:
253
+
254
+ .. code-block :: python
255
+
256
+ >> > class Person (models .Base ):
257
+ ...
258
+ ... name = fields.StringField(
259
+ ... required = True ,
260
+ ... validators = [
261
+ ... validators.Regex(' ^[A-Za-z]+$' ),
262
+ ... validators.Length(3 , 25 ),
263
+ ... ],
264
+ ... )
265
+ ... age = fields.IntField(
266
+ ... nullable = True ,
267
+ ... validators = [
268
+ ... validators.Min(18 ),
269
+ ... validators.Max(101 ),
270
+ ... ]
271
+ ... )
272
+ ... nickname = fields.StringField(
273
+ ... required = True ,
274
+ ... nullable = True
275
+ ... )
276
+ ...
277
+
278
+ >> > def only_odd_numbers (item ):
279
+ ... if item % 2 != 1 :
280
+ ... raise validators.ValidationError(" Only odd numbers are accepted" )
281
+ ...
282
+ >> > class Person (models .Base ):
283
+ ... lucky_numbers = fields.ListField(int , item_validators = [only_odd_numbers])
284
+ ... item_validator_str = fields.ListField(
285
+ ... str ,
286
+ ... item_validators = [validators.Length(10 , 20 ), validators.Regex(r " \w + " )],
287
+ ... validators = [validators.Length(1 , 2 )],
288
+ ... )
289
+ ...
290
+ >> > Person.to_json_schema()
291
+ {
292
+ " type" : " object" ,
293
+ " additionalProperties" : false,
294
+ " properties" : {
295
+ " item_validator_str" : {
296
+ " type" : " array" ,
297
+ " items" : {
298
+ " type" : " string" ,
299
+ " minLength" : 10 ,
300
+ " maxLength" : 20 ,
301
+ " pattern" : " /\\ w+/"
302
+ },
303
+ " minItems" : 1 ,
304
+ " maxItems" : 2
305
+ },
306
+ " lucky_numbers" : {
307
+ " type" : " array" ,
308
+ " items" : {
309
+ " type" : " number"
310
+ }
311
+ }
312
+ }
313
+ }
314
+
315
+ (Note that `only_odd_numbers ` did not modify schema, since only class based validators are
316
+ able to do that, though it will still work as expected in python. Use class based validators
317
+ that can be expressed in json schema if you want to be 100% correct on schema side.)
253
318
254
319
* Lazy loading, best for circular references:
255
320
@@ -331,6 +396,40 @@ Features
331
396
}
332
397
}
333
398
399
+ * Dealing with schemaless data
400
+
401
+ (Plese note that using schemaless fields can cause your models to get out of control - especially if
402
+ you are the one responsible for data schema. On the other hand there is usually the case when incomming
403
+ data are with no schema defined and schemaless fields are the way to go.)
404
+
405
+ .. code-block :: python
406
+
407
+ >> > class Event (models .Base ):
408
+ ...
409
+ ... name = fields.StringField()
410
+ ... size = fields.FloatField()
411
+ ... extra = fields.DictField()
412
+
413
+ >> > Event.to_json_schema()
414
+ {
415
+ " type" : " object" ,
416
+ " additionalProperties" : false,
417
+ " properties" : {
418
+ " extra" : {
419
+ " type" : " object"
420
+ },
421
+ " name" : {
422
+ " type" : " string"
423
+ },
424
+ " size" : {
425
+ " type" : " float"
426
+ }
427
+ }
428
+ }
429
+
430
+ `DictField ` allow to pass any dict of values (`"type": "object" `), but note, that it will not make any validation
431
+ on values except for the dict type.
432
+
334
433
* Compare JSON schemas:
335
434
336
435
.. code-block :: python
0 commit comments