Skip to content

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

Closed
@changk99

Description

@changk99

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

Activity

changed the title [-]the regexp keyword Unexpected behavior[/-] [+]the regexp keyword cashes pattern without taking flags into account[/+] on Nov 21, 2021
epoberezkin

epoberezkin commented on Nov 21, 2021

@epoberezkin
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)

34 remaining items

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @epoberezkin@changk99

        Issue actions

          the regexp keyword cashes pattern without taking flags into account · Issue #220 · ajv-validator/ajv-keywords