-
-
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
Possible rounding issue causing multipleOf to fail for a valid value. #1039
Comments
possible fix
|
Issue was resolved by using a different validation method, closing |
@mayafox I don't think that the issue is resolved by avoiding the bug. Would you be able to re-open the issue? |
It seems that the distinction between validation of floating point numbers and validation of arbitrary decimal precision is causing frequent confusion for users of
|
@cwegener I hit this issue while contributing to netbox-community/devicetype-library#1067 and the schema validation failing. Devs for that repo changed the validation method to multipleOfPrecision, and addressed the issue. |
Thanks for the additional info. I don't actually think that the netbox-community project even resolved the issue. The apparent "fix" is not a fix at all. netbox-community/devicetype-library#1068 I've left a comment in your PR thread in the netbox-community org netbox-community/devicetype-library#1067 (comment) |
@cwegener Ok, what's the plan of action then? I thought about the code snippet I proposed, and it's nowhere near the quality of what I would accept in my day job, but is potentially a starting point. |
Hi there -- please have a look at some of the older issues you can find by searching multipleOf -- essentially automatically rounding isn't the right thing to do for the same reason Python doesn't try to guess what you mean by doing |
Thank you for the heads up @cwegener do you have any recommendations on how to resolve this properly then? In my searches I have not found a well defined solution to this use case, but I did find a lot of other people suffering from the same confusion @Julian can you describe how to use this? I was checking here, and I do not see decimal defined. I could be missing it though. I had noticed someone else mention
|
Do the docs here happen to help? The example there is for |
Or I guess it sounds like you want info on how to do this with the |
@Julian yes, we are using the I seem to still be having the same issues here. I changed the
Using a value of
Do I need to also create the custom validator as well? I am sorry if this is a simple issue that I am overlooking, I appreciate the input. |
That should be it -- the exception there might make it seem you may not have saved perhaps? Or not have loaded both floats as decimals? But e.g. here's what should be a fuller example: import decimal
import json
schema = json.loads("""{"type": "number", "minimum": 0, "multipleOf": 0.01}""", parse_float=decimal.Decimal)
good = json.loads("10.11", parse_float=decimal.Decimal)
bad = json.loads("10.115", parse_float=decimal.Decimal)
jsonschema.validate(good, schema) # fine vs
->
|
Still getting the same thing, I will have to dig into this further. Thanks for all of your help so far |
So @Julian when checking my
Then it fails at Any ideas what I might have wrong here? Is it because we are using |
I am also seeing these errors now:
Which seems to lead back to jsonschema/jsonschema/_validators.py Line 202 in 65afdce
Does this warrant a new issue? |
That error means you've now properly loaded your schema with decimals, but not your instance (the thing you're validating). As I mentioned, you need to make sure both of them are decimals -- I'm not sure where you're getting your instance from though so I can't point out the error, but you need to load your instance JSON with the same |
@Julian So, turns out that loading the schema with Maybe you could shine some light if I overengineered something there or maybe improve the docs based on our troubles :D |
Ah I see, sorry, I had it reversed then, your instance was fine but not your schema -- I didn't realize you were saying you were loading the schema from behind a And definitely the docs could use more examples here but sometime soon (maybe in the next month or so) there'll be an even easier way to do this once support for the new referencing library lands which essentially will supercede Feedback definitely still appreciated though! |
Thanks for all of your help @Julian |
* Removed mgmt_only: false, removed extra newlines * Testing a fix for the multipleOf issue using info in python-jsonschema/jsonschema#1039 * Revert "Testing a fix for the multipleOf issue using info in python-jsonschema/jsonschema#1039" This reverts commit ea7edcc. * removing device-types/Dell/Unity_XT_DAE.yaml - there are multiple different models of these disk array enclosures. with the possible addition of storage management we need the actual part/models rather than a generalized item * Renamed Dell PowerSwitches to have the proper name Added data sheet/spec sheet links when available * Added data sheet links to comment * Update NX3240 to match proper name Standardized slug * Standardized names and slugs Added spec sheets where available
* Removed mgmt_only: false, removed extra newlines * Testing a fix for the multipleOf issue using info in python-jsonschema/jsonschema#1039 * Revert "Testing a fix for the multipleOf issue using info in python-jsonschema/jsonschema#1039" This reverts commit ea7edcc. * removing device-types/Dell/Unity_XT_DAE.yaml - there are multiple different models of these disk array enclosures. with the possible addition of storage management we need the actual part/models rather than a generalized item * Renamed Dell PowerSwitches to have the proper name Added data sheet/spec sheet links when available * Added data sheet links to comment * Update NX3240 to match proper name Standardized slug * Standardized names and slugs Added spec sheets where available
* Removed mgmt_only: false, removed extra newlines * Testing a fix for the multipleOf issue using info in python-jsonschema/jsonschema#1039 * Revert "Testing a fix for the multipleOf issue using info in python-jsonschema/jsonschema#1039" This reverts commit ea7edcc. * removing device-types/Dell/Unity_XT_DAE.yaml - there are multiple different models of these disk array enclosures. with the possible addition of storage management we need the actual part/models rather than a generalized item * Renamed Dell PowerSwitches to have the proper name Added data sheet/spec sheet links when available * Added data sheet links to comment * Update NX3240 to match proper name Standardized slug * Standardized names and slugs Added spec sheets where available
* Removed mgmt_only: false, removed extra newlines * Testing a fix for the multipleOf issue using info in python-jsonschema/jsonschema#1039 * Revert "Testing a fix for the multipleOf issue using info in python-jsonschema/jsonschema#1039" This reverts commit ea7edcc. * removing device-types/Dell/Unity_XT_DAE.yaml - there are multiple different models of these disk array enclosures. with the possible addition of storage management we need the actual part/models rather than a generalized item * Renamed Dell PowerSwitches to have the proper name Added data sheet/spec sheet links when available * Added data sheet links to comment * Update NX3240 to match proper name Standardized slug * Standardized names and slugs Added spec sheets where available
Encountered the following error when fixing the schema validation errors for a new device I was adding into netbox/devicetype-library
Failed validating 'multipleOf' in schema['properties']['weight']['items']['properties']['value']:
{'minimum': 0, 'multipleOf': 0.01, 'type': 'number'}
On instance['weight'][0]['value']:
10.11
Did a bit of poking around, changing the value to 1011 <<< integer, and not a float, caused the check to pass. This seems to potentially be an issue in jsonschema,
https://github.com/python-jsonschema/jsonschema/blob/main/jsonschema/_validators.py line 181
If I am reading the code right, jsonschema is running into a rounding error when running this validation.
db = 0.01
instance = 10.11
isinstance(db, float)
True
quotient = instance / db
quotient
1010.9999999999999
int(quotient)
1010
int(round(quotient, 2))
1011
Any Thoughts, additional information I should collect?
The text was updated successfully, but these errors were encountered: