-
-
Notifications
You must be signed in to change notification settings - Fork 877
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
Allow pattern to also work with number/integer type #2333
Comments
The data is valid because you have |
In principle what you are trying to achieve (the limit the precision of a number) should be possible with const schema = {
type: 'object',
properties: {
price: {
type: 'number',
multipleOf: 0.01
}
}
};
const validate = ajv.compile(schema)
function test(data) {
const valid = validate(data)
if (valid) console.log(data.price + " is valid!")
else console.log(data.price + " is invalid: " + ajv.errorsText(validate.errors))
}
test({price: 10.04})
// "10.04 is invalid: data/price must be multiple of 0.01"
test({price: -10.06})
// "-10.06 is valid!"
test({price: 123.005})
// "123.005 is invalid: data/price must be multiple of 0.01" |
@sebix it doesn't work for 123.50001 for example, see: https://replit.com/@caub/ajv-currency#index.js |
What version of Ajv you are you using?
Latest: 8.12.0
What problem do you want to solve?
pattern
prop is ignored if type isnumber
for exampleWhat do you think is the correct solution to problem?
pattern
should still be used, as for exampleregex.test(value)
works if value is a string or number, it's automatically stringifiedWill you be able to implement it?
probably
Code summary
Of course it's possible to add a custom keyword to get it working, but would be great if it works in the core
The text was updated successfully, but these errors were encountered: