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

maxLength and minLength do not work when a number with a zero decimal is used. #446

Closed
mohsin-sq opened this issue Sep 15, 2021 · 3 comments

Comments

@mohsin-sq
Copy link
Contributor

According to the json spec

1.0 and 1 are both considered integers, however the method used in max/minLength validations isIntegralNumber does not return true for 1.0. This has caused us some headaches as we are using proto3 to go from json -> proto3 and are running into the issue where when we set maxLength value to be 2, it get converted to 2.0 and does not get evaluated by the validators: https://github.com/networknt/json-schema-validator/blob/master/src/main/java/com/networknt/schema/MaxLengthValidator.java#L36

There was a recent issue where in jackson-databind which addressed this isIntegralNumber method and an canConvertToExactIntegral method was created for this exact purpose which should return true for both 2.0 and 2.

We propose updating the validators (something i can also do and send a pull request) where isIntegralNumber is used in the length validators (7 locations: https://github.com/networknt/json-schema-validator/search?q=isIntegralNumber)

References:
https://json-schema.org/understanding-json-schema/reference/numeric.html
FasterXML/jackson-databind#2885

@stevehu
Copy link
Contributor

stevehu commented Sep 17, 2021

@mohsin-sq I agree with you to consider 2.0 as 2 in the maxLength and minLength validators. Would you be able to summit a PR to get it implemented? Thanks.

@mohsin-sq
Copy link
Contributor Author

I have created a PR: #450

@mohsin-sq
Copy link
Contributor Author

Thanks for merging that @stevehu!

When do you think it'll make it into maven central?

@stevehu stevehu closed this as completed Oct 10, 2021
stevehu pushed a commit that referenced this issue Jan 14, 2022
* Improve type validation of integrals

Instead of doing string comparison, use new jackson method to determine if a number is an integral.

The `javaSemantics` config option was added in PR #343 which partially addressed issue #334. In the notes for this PR:
> Once jackson-databind 2.12.0 is out, I'll replace my solution with a call to canConvertToExactIntegral

jackson-databind has been updated to 2.12.1 so this is available but the change has not yet been made.

PR #450 which addressed #446 missed this location which is used when calling `JsonSchemaFactory.getSchema`.

Issue #344 requested coercion of various types but the only type implemented in PR #379 was lossless narrowing, set with configuration option `losslessNarrowing`. I believe that setting is unnecessary now as this implementation of `javaSemantics` addresses the original issue, but left it for backwards compatibility. It also allows you to set `javaSemantics=false` and `losslessNarrowing=true` to achieve only this specific case rather than anything that `javaSemantics` is used for in the future. At this time, these properties do exactly the same thing.

- Change from string comparison to `canConvertToExactIntegral` for `javaSemantics` and `losslessNarrowing`
- Add missing documentation around `losslessNarrowing`
- Add more test cases around integrals

* Update changelog
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