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

Allow pattern to also work with number/integer type #2333

Closed
caub opened this issue Sep 28, 2023 · 4 comments
Closed

Allow pattern to also work with number/integer type #2333

caub opened this issue Sep 28, 2023 · 4 comments

Comments

@caub
Copy link
Contributor

caub commented Sep 28, 2023

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 is number for example

What do you think is the correct solution to problem?
pattern should still be used, as for example regex.test(value) works if value is a string or number, it's automatically stringified

Will you be able to implement it?
probably

Code summary

const Ajv = require('ajv').default;

const ajv = new Ajv({
  coerceTypes: true,
});
const schema = {
  type: 'object',
  properties: {
    price: {
      type: 'number',
      pattern: '^-?\\d+(\\.\\d{1,2})?$'
    }
  }
};
// this shouldn't be valid, yet it is
console.log(
  ajv.validate(schema, { price: '123.005' }),
  ajv.errors,
);

Of course it's possible to add a custom keyword to get it working, but would be great if it works in the core

@jasoniangreen
Copy link
Collaborator

The data is valid because you have coerceTypes which changes it to a number and then with number the pattern keyword is ignored. I don't see this being something we would consider changing as it is defined by the JSON Schema standard in this way.

@sebix
Copy link

sebix commented Jun 21, 2024

In principle what you are trying to achieve (the limit the precision of a number) should be possible with multipleOf, but that shows interesting behavior:

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
Copy link

sebix commented Jun 21, 2024

Ah, @caub: just use

const ajv = new Ajv({
    allErrors: true,
    multipleOfPrecision: 2
})

then multipleOf: 0.01 works fine.

Found via #84, #2416 and #652 the latter also has alternative solutions.

@caub
Copy link
Contributor Author

caub commented Jul 24, 2024

@sebix it doesn't work for 123.50001 for example, see: https://replit.com/@caub/ajv-currency#index.js

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

No branches or pull requests

4 participants
@sebix @caub @jasoniangreen and others