File tree 2 files changed +31
-3
lines changed
python_jsonschema_objects
2 files changed +31
-3
lines changed Original file line number Diff line number Diff line change
1
+ import decimal
1
2
import logging
2
3
3
4
import six
@@ -40,8 +41,14 @@ def __call__(self, name):
40
41
41
42
@registry .register ()
42
43
def multipleOf (param , value , _ ):
43
- quot , rem = divmod (value , param )
44
- if rem != 0 :
44
+ # This conversion to string is intentional because floats are imprecise.
45
+ # >>> decimal.Decimal(33.069)
46
+ # Decimal('33.0690000000000026147972675971686840057373046875')
47
+ # >>> decimal.Decimal('33.069')
48
+ # Decimal('33.069')
49
+ value = decimal .Decimal (str (value ))
50
+ divisor = decimal .Decimal (str (param ))
51
+ if value % divisor != 0 :
45
52
raise ValidationError ("{0} is not a multiple of {1}" .format (value , param ))
46
53
47
54
Original file line number Diff line number Diff line change @@ -41,7 +41,7 @@ def test_schema_validation():
41
41
"$id" : "test" ,
42
42
"type" : "object" ,
43
43
"properties" : {
44
- "name" : "string" , # <-- this is invalid
44
+ "name" : "string" , # <-- this is invalid
45
45
"email" : {"oneOf" : [{"type" : "string" }, {"type" : "integer" }]},
46
46
},
47
47
"required" : ["email" ],
@@ -531,3 +531,24 @@ def test_justareference_example(markdown_examples):
531
531
)
532
532
ns = builder .build_classes ()
533
533
ns .JustAReference ("Hello" )
534
+
535
+
536
+ def test_number_multiple_of_validation ():
537
+ schema = {
538
+ "$schema" : "http://json-schema.org/schema#" ,
539
+ "$id" : "test" ,
540
+ "type" : "object" ,
541
+ "title" : "Base" ,
542
+ "properties" : {
543
+ "sample" : {
544
+ "type" : "number" ,
545
+ "minimum" : 0 ,
546
+ "maximum" : 1000000000 ,
547
+ "multipleOf" : 0.001 ,
548
+ },
549
+ },
550
+ }
551
+
552
+ builder = pjs .ObjectBuilder (schema )
553
+ ns = builder .build_classes ()
554
+ ns .Base (sample = 33.069 )
You can’t perform that action at this time.
0 commit comments