Skip to content
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

float number, range validation failure #1274

Closed
trolldbois opened this issue Jul 1, 2024 · 4 comments
Closed

float number, range validation failure #1274

trolldbois opened this issue Jul 1, 2024 · 4 comments

Comments

@trolldbois
Copy link

It seems that jsonschema fails to properly validate number type in a range.

cve-short-test-fail.json
cve-short-test-success.json
number_float_schema.json

When using jsonschema to validate float number in a range, the validation fails.
Attached

  • number_float_schema.json , the schema (validation of a float between 4 and 6.9 )
  • cve-short-test-success.json , the data that passes validation (5.7)
  • cve-short-test-fail.json , the data that fails validation (5.6) with error "5.6: 5.6 is not a multiple of 0.1"

the failure tested on python 3.12.4 , jsonschema 4.22 (screenshots attached )
jsonschema-number-float-validation-fail
jsonschema-number-float-validation-fail-schema

Schema was tested successfully, no validation error in

@Julian
Copy link
Member

Julian commented Jul 1, 2024

Hi there. You haven't provided your code so it's not a sure thing, but I'd bet you've used float division (as it's the default when loading JSON) and expecting non-float behavior. For floats, 5.6 indeed is not divisible by 0.1.

If you don't want that, don't use floats, e.g.:

~[jsonschema:python] -c '
quote> from pathlib import Path                                                     
import decimal
import json

import jsonschema.validators


def load(path):
    return json.loads(path.read_text(), parse_float=decimal.Decimal)


fail = load(Path("cve-short-test-fail.json"))
success = load(Path("cve-short-test-success.json"))
schema = load(Path("number_float_schema.json"))

Validator = jsonschema.validators.validator_for(schema)
validator = Validator(schema)
print(validator.is_valid(fail))
print(validator.is_valid(success))
'
True
True

(I thought I'd added this to the FAQ, but it's certainly covered in quite a few previous issues. I'll add it at some point.)

@Julian Julian closed this as not planned Won't fix, can't repro, duplicate, stale Jul 1, 2024
@trolldbois
Copy link
Author

Thank you for the fast answer @Julian
I used the jsonschema module main command line for the validation.

python -m jsonschema -i cve-short-test-fail.json number_float_schema.json

I also tried the same with check-jsonschema , same result.

I can see how this could be solved.
thanks

@Julian
Copy link
Member

Julian commented Jul 1, 2024

I'm not as familiar with check-jsonschema as one might expect, but I'd either look for an option there to configure how it deserializes JSON or else in the event it doesn't yet have one, open an issue.

@trolldbois
Copy link
Author

Thanks, I tried your proposal on the original codebase (cvelib), and it fixes the issue.

Many thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants