JSON (schema) validation with escaped characters in patterns fails

13.4k views Asked by At

The following JSON object is valid:

{
    "foo": "bar",
    "pattern": "^(\/?[-a-zA-Z0-9_.]+)+$"
}

Whereas this one is not:

{
    "foo": "bar",
    "pattern": "^(\/?[-a-zA-Z0-9_.]+)+\.jpg$"
}

It's the escaped dot (\.), but I can't see why this should not be valid JSON. I need to include such patterns in my real JSON schemas. The regexp there are far more complex and there is no way missing out on excaping, especially the dot.

BTW, escaping hypens in character classes such as in [a-z\-] breaks validation as well.

How do I fix that?

Edit: I used http://jsonlint.com and a couple of node libraries.

2

There are 2 answers

0
evanmcdonnal On BEST ANSWER

You need to double escape here. The slash is an escape character in json so you can't escape the dot (as it sees it) instead you need to escape that backslash so your regex comes out with \. like it should (json is expecting a reserved character after the escape ie a quote or another slash or something).

// passes validation
{
    "foo": "bar",
    "pattern": "^(/?[-a-zA-Z0-9_.]+)+\\.jpg$"
}
0
rofrol On

You can use regexp from ajv-keywords

import Ajv from 'ajv';
import AjvKeywords from 'ajv-keywords';
// ajv-errors needed for errorMessage
import AjvErrors from 'ajv-errors';

const ajv = new Ajv.default({ allErrors: true });

AjvKeywords(ajv, "regexp");
AjvErrors(ajv);

// modification of regex by requiring Z https://www.regextester.com/97766
const ISO8601UTCRegex = /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(.[0-9]+)?Z$/;

const typeISO8601UTC = {
  "type": "string",
  "regexp": ISO8601UTCRegex.toString(),
  "errorMessage": "must be string of format 1970-01-01T00:00:00Z. Got ${0}",
};

const schema = {
  type: "object",
  properties: {
    foo: { type: "number", minimum: 0 },
    timestamp: typeISO8601UTC,
  },
  required: ["foo", "timestamp"],
  additionalProperties: false,
};

const validate = ajv.compile(schema);

const data = { foo: 1, timestamp: "2020-01-11T20:28:00" }

if (validate(data)) {
  console.log(JSON.stringify(data, null, 2));
} else {
  console.log(JSON.stringify(validate.errors, null, 2));
}

https://github.com/rofrol/ajv-regexp-errormessage-example