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

Open
caub opened this issue Sep 28, 2023 · 0 comments
Open

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

caub opened this issue Sep 28, 2023 · 0 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

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

No branches or pull requests

2 participants
@caub and others