gecBurton / djangorestframework-jsonschema

djangorestframework-jsonschema
ISC License
6 stars 0 forks source link

`multipleOf` is not supported #1

Open gecBurton opened 4 years ago

gecBurton commented 4 years ago

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

  1. the use of the decimal_places argument to ensure that only the right number of digits is being passed
  2. the use of a bespoke multipleOf validator to enforce the right multiplicity
jacksmith15 commented 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
gecBurton commented 4 years ago

@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

MattOates commented 4 years ago

@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
gecBurton commented 4 years ago

ahhhhh good point @MattOates !