-
-
Notifications
You must be signed in to change notification settings - Fork 582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
multipleOf incorrectly raises ValidationError for some floats #320
Comments
I'd recommend not using floats to recommend dollar amounts in that case. You could use a string and a simple regex fairly easily I think? |
Alternatively you can deserialize your JSON using decimals as well. |
Closing this out, but lemme know if you disagree. |
If we use Decimal in deserialization we get an error:
so multipleof is unusable... This is how I did to workaround:
If you want I can submit a merge request. This feature is required for my purpose, however I would prefer to use the official package than a fork. |
Sounds like you only used a decimal in one of the two places, you need to
have decimals both in your schema and in your instance.
On Jul 13, 2017 13:25, "Fufutako" <[email protected]> wrote:
If we use Decimal in deserialization we get an error:
jsonschema/_validators.py", line 124, in multipleOf
quotient = instance / dB
TypeError: unsupported operand type(s) for /: 'decimal.Decimal' and 'float```
so multipleof is unusable...
This is how I did to workaround:
def multipleOf(validator, dB, instance, schema):
if not validator.is_type(instance, "number"):
return
if isinstance(dB, float):
if isinstance(instance, float):
quotient = instance / dB
else:
quotient = instance / Decimal(str(dB))
failed = int(quotient) != quotient
else:
failed = instance % dB
if failed:
yield ValidationError("%r is not a multiple of %r" % (instance, dB))```
If you want I can submit a merge request. This feature is required for my
purpose, however I would prefer to use the official package than a fork.
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#320 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAUIXk3muj_9vAHXKiOYJNXKR6GA2XAuks5sNf6ngaJpZM4L5RuN>
.
|
Hi, Thanks for your reply. This is an extract from the json-schema.
it is stored in db and passed directly to validate. So I have no control on dB expected by the schema. How can I make my json-schema to get a Decimal for 'price' ? multipeof is a key word related to number so I don't understand how I can do that. In another hand instance when is deserialized to a float produce floating point error and when it is deserialized to a Decimal raise a TypeError. |
I presume you deserialize out if your DB using the json module? (You have
to be deserializing using *something*) -- json.loads and friends take
arguments which affect what Python type is used to deserialize JSON
numbers, so you need to pass Decimal to those.
On Jul 13, 2017 14:16, "Fufutako" <[email protected]> wrote:
Hi,
Thanks for your reply.
This is an extract from the json-schema.
'offer_items': {'items': {'maxProperties': 4,
'minItems': 1,
'properties': {'description': {'maxLength': 40,
'type': 'string'},
'price': {'multipleOf': 0.01,
'type': 'number'},
'product_id': {'maxLength': 20,
'pattern': '[0-9]+',
'type': 'string'},
'quantity': {'type':
'integer'}},
'required': ['product_id',
'description',
'quantity',
'price'],
'title': 'offer_item',
'type': 'object'}
it is stored in db and passed directly to validate. So I have no control on
dB expected by the schema. How can I make my json-schema to get a Decimal
for 'price' ? multipeof is a key word related to number so I don't
understand how I can do that. In another hand instance when is deserialized
to a float produce floating point error and when it is deserialized to a
Decimal raise a TypeError.
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#320 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAUIXvvcJJ4otd2vM7koKT-y97M4oXZTks5sNgqbgaJpZM4L5RuN>
.
|
Yes... I use a json field in Postgres and sqlalchemy.JSONField to deal with. I am looking How can I do that with this package. Thanks |
In
_validators.py
, themultipleOf
function incorrectly raises a ValidationError for e.g. 606.67 as not a multiple of 0.01, due to floating point errors.Our use case is using a schema to ensure that amounts (in Dollars) are in increments of 1 cent ($0.01)
Going through the code,
One number is a multiple of another number when the quotient is an integer. However, in this case, the quotient is very close to an integer due to floating point representation of those numbers.
The solution I see is:
instance
is a multiple ofdb
ifquotient
is close to an integer.I'm happy to submit a pull request, and first would like to
The text was updated successfully, but these errors were encountered: