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

Detailed pattern violation errors #2332

Open
Pamplemousse opened this issue Sep 26, 2023 · 0 comments
Open

Detailed pattern violation errors #2332

Pamplemousse opened this issue Sep 26, 2023 · 0 comments

Comments

@Pamplemousse
Copy link

What version of Ajv you are you using?

8.12.0

What problem do you want to solve?

More explicit error details when using pattern on string properties.

Current behaviour:

# test.js
import Ajv2020 from 'ajv/dist/2020.js';
import addFormats from 'ajv-formats';

import schema from './schema.json' assert { type: 'json' };
import data from './data.json' assert { type: 'json' };

const ajv = new Ajv2020({ allErrors: true, strict: false });
addFormats(ajv);

const validate = ajv.compile(schema);
const result = validate(data);

if (!result) {
  console.error(validate.errors);
  process.exit(1)
}
{
  "$id": "my-schema",
  "$schema": "http://json-schema.org/schema",

  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "pattern": "^f.*z$"
    }
  }
}
{
  "name": "oobarba"
}
$ node test.js
[
  {
    instancePath: '/name',
    schemaPath: '#/properties/name/pattern',
    keyword: 'pattern',
    params: { pattern: '^f.*z$' },
    message: 'must match pattern "^f.*z$"'
  }
]

What do you think is the correct solution to problem?

It would be nice to show the location in the string where the pattern is violated.

I couldn't find any JS API that would return errors when matching a string from a regex.
However, it seems that negating (not sure if that's generalizable) a given regex could do the trick:

const regexContent = "f.*z"

const regex = new RegExp(`^${regexContent}$`);
console.log("foobarbaz".match(regex));
console.log("oobarba".match(regex));

const invertedRegex = new RegExp(`^(?!.*${regexContent})`);
console.log("foobarbaz".match(invertedRegex));
console.log("oobarba".match(invertedRegex));
$ node test.js 
[ 'foobarbaz', index: 0, input: 'foobarbaz', groups: undefined ]
null
null
[ '', index: 0, input: 'oobarba', groups: undefined ]
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
@Pamplemousse and others