ropensci / jsonvalidate

:heavy_check_mark::interrobang: Validate JSON
https://docs.ropensci.org/jsonvalidate
Other
48 stars 14 forks source link

Regex flag when specifying a pattern #36

Open eric-bonucci opened 4 years ago

eric-bonucci commented 4 years ago

Hi,

When specifying a pattern using a regular expression, one can not use the case insensitive flag in the regex.

Reproducible example (using dplyr):

library(dplyr)
'
{
    "type": "object",
    "properties":{
      "product_type": {
      "type": ["string"],
      "pattern": "^(?i)test$"
    }
    },
  "required": [
      "product_type"
  ],
}
' %>% jsonvalidate::json_validator(engine = "ajv") -> validator
richfitz commented 2 years ago

Sorry for missing this issue for a year :scream:

This appears to be an issue with the underling Ajv engine:

const Ajv = require('ajv');
const opts = {allErrors: true,
            verbose: true,
            unicodeRegExp: true,
            strict: true};
const obj = new Ajv(opts);
const schema = {
    "type": "object",
    "properties":{
    "product_type": {
            "type": ["string"],
            "pattern": "^(?i)test$"
        }
    },
    "required": [
    "product_type"
    ]
}
obj.compile(schema)

which errors with

Thrown:
SyntaxError: Invalid regular expression: /^(?i)test$/: Invalid group

so the error reported is faithful to the underlying engine.

I found this issue on the Ajv repo: https://github.com/ajv-validator/ajv/issues/101, though the link within to "defining custom keywords" is broken - I think this is the relevant page now: https://ajv.js.org/keywords.html

In short, this looks quite hard to support unfortunately