Open gecBurton opened 4 years ago
class MultipleOf:
def __init__(self, factor):
self.factor = factor
def __call__(self, value):
if isinstance(factor, float):
quotient = instance / factor
failed = int(quotient) != quotient
else:
failed = instance % factor
if failed:
raise ValidationError
@jacksmith15 why the special handling for floats?
assert 9.0 % 3 == 0
assert 9.0 % 3.0 == 0
assert 9.0 % 3 == 0.0
assert 9 % 3 == 0.0
cc @LorenaLorene
@gecBurton let me float this one by you...
> assert 0.3 % 0.2 == 0.1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
> 0.3 % 0.2
0.09999999999999998
ahhhhh good point @MattOates !
multipleOf is one of the key features of the jsonschema number types and is currently not supported in this code.
We are mapping the jsonschema-number number to django-DecimalField here but this should be extended to include
decimal_places
argument to ensure that only the right number of digits is being passedmultipleOf
validator to enforce the right multiplicity