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

the regexp keyword cashes pattern without taking flags into account #220

Closed
changk99 opened this issue Sep 26, 2021 · 1 comment
Closed
Labels

Comments

@changk99
Copy link

ajv: '^8.6.3'
ajv-keywords: '^5.0.0'

const Ajv = require('ajv').default
const ajv = new Ajv()
const ajvKeywords = require('ajv-keywords')
ajvKeywords(ajv)
const stringSchema = {
  type: 'string',
  pattern: 'a'
}
const stringSchema3 = {
  $id: 'regexp',
  type: 'string',
  regexp: {
    pattern: 'a',
    flags: 'i'
  }
}
console.log(ajv.validate(stringSchema, "a")) // true
console.log(ajv.validate(stringSchema3, "A")) // false

I expect the second logger to output true

@epoberezkin epoberezkin changed the title the regexp keyword Unexpected behavior the regexp keyword cashes pattern without taking flags into account Nov 21, 2021
@epoberezkin
Copy link
Member

this is specifically caused by the presence of the first schema, the regexp keyword looks up patterns in the same dictionary, by pattern, without taking flags into account... So this would do the same:

const Ajv = require('ajv').default
const ajv = new Ajv()
const ajvKeywords = require('ajv-keywords')
ajvKeywords(ajv)
const stringSchema1 = {
  type: 'string',
  regexp: {
    pattern: 'a'
  }
}
const stringSchema2 = {
  $id: 'regexp',
  type: 'string',
  regexp: {
    pattern: 'a',
    flags: 'i'
  }
}
console.log(ajv.validate(stringSchema1, "a")) // true
console.log(ajv.validate(stringSchema1, "A")) // false
console.log(ajv.validate(stringSchema2, "A")) // false, even though it should be true (and it is true in case there is no first schema)

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

No branches or pull requests

2 participants