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
NaN validates as integer, ignoring minimum #182
Comments
Outside of spec I'd say... JSON-schema is for JSON validation. NaN is not JSON. |
It shouldn't be supported – it's not a valid number, so it should fail. |
By "supporting" I meant "failing" :) |
I would expect that schema to fail with the input object, because the value, whatever it is, is not an integer. Wouldn't you expect the same? |
|
I will fix this particular issue. I agree that NaN should not be considered I was just trying to say that supporting NaNs (= returning "expected" results) is part of the bigger issue of supporting all JavaScript types/primitives in "expected" way. In some cases expectations will vary. Why JSON-schema as a standard is defined for validating JSON; in JavaScript terms - the result of JSON.parse(). So how a validator should behave with NaN or any other language-specific artefact is outside of spec - ajv is definitely not the only one that will pass NaN as an integer. I am sure there are other edge cases that would return unexpected results - there are no tests for them at the moment. |
If it were to fail |
in 4.1.0 |
Thank you @epoberezkin ! |
@epoberezkin Could you elaborate a little more on why you chose to allow If I understand correctly I can use a One option I could see if you don't want to just flat out say |
I fully agree with @achrist on this! If it's not a valid JSON value, like If there's any "backwards compatibility" concern, then this feature could certainly be introduced as an option, like |
If your data is JSON then there would be no NaNs. Given that Ajv validates arbitrary objects, it has to make decisions about how to validate everything that's non JSON. Following your logic, everything that cannot be JSON should be validated as invalid, that would have been a very limiting design choice - users use Ajv for a lot of validation scenarios beyond JSON validation.
A better approach is to prevent NaNs passed to Ajv in the first place. NaN can only appear as the result of some expression, and the use case for validation is to do it before any other data manipulation, so it's not quite clear why would you get NaNs into your data in the first place.
I will think about it. I am not convinced that validating computation results/NaNs is a common enough use case that justifies one more option - there are way too many already. |
@epoberezkin
Then:
Which mean that Ajv works only with valid JSON and your first statement then makes no sense. A problem that currently have is that I need to run NEW validation library on top of the Ajv just to convert non-JSON data to JSON data in order to be sure Ajv will catch all errors. I'm okay if that's a case but I would like to have some buildin function even if I need to run it separately or some additional parameter for a constructor. |
Exactly - I am using In my case it would have eased my debugging experience if |
We have "found" this problem as well. We are using ajv to validate objects sent to and loaded from blob storage. Our usecase is
To make matters worse our field has a type Needles to say it took us quite some time to find what is happening.. Its completely unexpected for NaN, -Infinity, +Infinity to validate as valid numbers, my quick test in the office found that 8 from 8 programmers would fail to write correct schema with this behaviour. As a workaround we have started using ajv-bsontype which will correctly fail validation with |
Looks like there are a few cases to validate NaN differently. I am ok with adding an option strictNumbers (false by default) which, if set, would lead to NaN and Infinity failing In the next major version the option can be flipped (become true by default). |
I've opened a PR to add this option #1219 |
NaN
should not be accepted as an integer, and does not meet theminimum: 1
condition.Is this per the spec, or a bug?
Thank you!
The text was updated successfully, but these errors were encountered: