Hey together,
i wasn't sure if this problem would fit inside https://discourse.nodered.org/t/json-node-schema-validation/2574/13
So i would like to validate json against a json schema.
The JSON node comes with the abillity to pass a schema via msg.schema
But somehow it seems not to work correctly for me. At least i got the feeling it can not always compile a valid schema.
I am using examples from https://json-schema.org/learn/miscellaneous-examples.html
{
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string",
"description": "The person's first name."
},
"lastName": {
"type": "string",
"description": "The person's last name."
},
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0
}
}
}
The error i am getting is "JSON Schema error: failed to compile schema"
But the interesting thing is... The very first time i pasted it in it worked.
So what i did:
- i created the flow below. Tested it and it worked.
- Then i changed the inject node and extend it with a
,"required": ["firstName"],
This was the firstTime the error message displays. - Then i copied the example again from the page and pasted it inside the inject node.
Nothing seems to change it back.
Does the schema validation do not recover from compile errors or am i doing something wrong ?
The payload is from another example but i expect to get schema validation errors and not compile errors.
Also i took a look inside the core node. The schemes added inside there is only draft-06.
Probably they can be updated to newer schemes. Somewhere i saw also a topic from the devs from this april about the json node but i can't find it for linking.
My Environment:
NodeRed 1.3.3
Node.js v14.16.0
Windows_NT 10.0.18363 x64 LE
flow:
[{"id":"28adefd8.91d96","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"fef64023.e8edf","type":"json","z":"28adefd8.91d96","name":"","property":"payload","action":"obj","pretty":false,"x":410,"y":180,"wires":[["660c0a7b.7bd554"]]},{"id":"7399e62e.fe6558","type":"inject","z":"28adefd8.91d96","name":"","props":[{"p":"schema","v":"{\"$id\":\"https://example.com/person.schema.json\",\"$schema\":\"https://json-schema.org/draft/2020-12/schema\",\"title\":\"Person\",\"type\":\"object\",\"properties\":{\"firstName\":{\"type\":\"string\",\"description\":\"The person's first name.\"},\"lastName\":{\"type\":\"string\",\"description\":\"The person's last name.\"},\"age\":{\"description\":\"Age in years which must be equal to or greater than zero.\",\"type\":\"integer\",\"minimum\":0}}}","vt":"json"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"longitude\":0,\"latitude\":90}","payloadType":"json","x":250,"y":180,"wires":[["fef64023.e8edf"]]},{"id":"660c0a7b.7bd554","type":"debug","z":"28adefd8.91d96","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":180,"wires":[]},{"id":"f40a8e83.298b6","type":"catch","z":"28adefd8.91d96","name":"","scope":null,"uncaught":false,"x":240,"y":400,"wires":[["798295a5.ba882c"]]},{"id":"7275d6ac.7c4868","type":"debug","z":"28adefd8.91d96","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":640,"y":400,"wires":[]},{"id":"798295a5.ba882c","type":"function","z":"28adefd8.91d96","name":"","func":"msg.payload=msg.error.message\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":400,"wires":[["7275d6ac.7c4868"]]}]
EDIT:
so i just deleted the $ chars inside the schema in the inject node. And the compile error is gone.
{
"id": "https://example.com/person.schema.json",
"schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string",
"description": "The person's first name."
},
"lastName": {
"type": "string",
"description": "The person's last name."
},
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0
}
}
}
But adding a required generic keyword inside, brings up the compile error again,
{
"id": "https://example.com/person.schema.json",
"schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Person",
"required": [
"firstName",
"lastName"
],
"type": "object",
"properties": {
"firstName": {
"type": "string",
"description": "The person's first name."
},
"lastName": {
"type": "string",
"description": "The person's last name."
},
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0
}
}
}