What is the correct way of validating a JSON document with values that contains line breaks, \n
, using ajv?
Simplified example:
- A JSON schema defines a document that has a single property called
key
that accepts astring
value (the schema was inferred by submitting{"key":"value"}
to https://jsonschema.net) - A JavaScript object is serialized using
JSON.stringify()
. As a consequence, newline characters such as\n
are escaped, i.e.\\n
- Ajv's validate() function is called to validate the serialized string
Snippet:
// JSON schema definition
const schema = {
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"title": "The Root Schema",
"required": [
"key"
],
"properties": {
"key": {
"$id": "#/properties/key",
"type": "string",
"title": "The Key Schema",
"default": "",
"examples": [
"value"
],
"pattern": "^(.*)$"
}
}
};
// a JavaScript object that has a line break
const data = { key: 'a string\nwith a line break' };
// serialize to JSON
const json = JSON.stringify(data);
// json === "{\"key\":\"a string\\nwith a line break\"}"
// validate
const Ajv = require('ajv');
const ajv = new Ajv();
const valid = ajv.validate(schema, json);
// print results
console.info('Valid:', valid);
console.info('Errors:', ajv.errors);
I expected this to work, but it turns out that validation fails during execution:
Valid: false
Errors: [
{
keyword: 'type',
dataPath: '',
schemaPath: '#/type',
params: { type: 'object' },
message: 'should be object'
}
]
To my understanding this is because json
is a string
whereas the schema definition states that it should be an object
.
I have also tried to deserialize the JSON string, e.g. ajv.validate(schema, JSON.parse(json));
, but it also fails:
Valid: false
Errors: [
{
keyword: 'pattern',
dataPath: '.key',
schemaPath: '#/properties/key/pattern',
params: { pattern: '^(.*)$' },
message: 'should match pattern "^(.*)$"'
}
]
This makes sense because JSON.parse()
returns a JavaScript object which is not JSON (e.g. keys are not quoted and importantly for this question string values with unescaped \n
characters).
Dependency: ajv 6.10.0