Just for those that come after me: I ended rolling my own validator function node because I wanted to check attributes on the msg
object. This isn't possible with the either the JSON node nor the msg-validator node because they can only validate a property of the msg
object.
So my request would be that if the JSON node is extended as a validator, then please allow validation on the top-level msg
object and not just on a property of the msg
.
This is a simple example of a schema and the home-grown function node:
[{"id":"9d516b0b284e8481","type":"template","z":"36690f145d5af6ca","name":"schema","field":"schema","fieldType":"msg","format":"json","syntax":"plain","template":"{\n \"title\": \"Email data required on the msg object\",\n \"type\": \"object\",\n \"required\": [\n \"email\"\n ],\n \"properties\": {\n \"email\": {\n \"type\": \"object\",\n \"required\": [\n \"to\",\n \"from\",\n \"subject\"\n ],\n \"properties\": {\n \"from\": {\n \"type\": \"string\",\n \"description\": \"Sender email.\"\n },\n \"from_name\": {\n \"type\": \"string\",\n \"description\": \"Senders full name.\"\n },\n \"to\": {\n \"type\": \"string\",\n \"description\": \"Receiver email.\"\n },\n \"cc\": {\n \"type\": \"string\",\n \"description\": \"CC email or blank.\"\n },\n \"bcc\": {\n \"type\": \"string\",\n \"description\": \"BCC email or blank.\"\n },\n \"subject\": {\n \"type\": \"string\",\n \"description\": \"Subject Line of email\"\n }\n }\n }\n }\n}","output":"json","x":372,"y":1091,"wires":[["9efdc1c898e037ec","62a6aee706773c33"]]},{"id":"a14424b157213fb4","type":"function","z":"36690f145d5af6ca","name":"JSON Schema validator","func":"var validator = new Ajv({\n allErrors: true,\n messages: true\n})\n\nconst validate = validator.compile(msg.schema)\n\nconst v = validate(msg);\n\nif ( !v ) {\n msg.errors = validate.errors;\n node.error(\"validation failed\", msg)\n} else {\n delete msg.schema;\n return msg;\n}","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"Ajv","module":"ajv"}],"x":774,"y":1091,"wires":[["88dad74cd25d507d"]],"outputLabels":["ok"]},{"id":"9efdc1c898e037ec","type":"function","z":"36690f145d5af6ca","name":"valid data","func":"msg.email = {\n cc: \"someemail\",\n to: \"asdsa\",\n from: \"dddd\",\n subject: \"hello world\"\n}\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":544,"y":1091,"wires":[["a14424b157213fb4"]]},{"id":"7eb33583b79df7f6","type":"inject","z":"36690f145d5af6ca","name":"Trigger","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":187,"y":1091,"wires":[["9d516b0b284e8481"]]},{"id":"88dad74cd25d507d","type":"debug","z":"36690f145d5af6ca","name":"msg is good","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"email","targetType":"msg","statusVal":"","statusType":"auto","x":1063,"y":1092,"wires":[]},{"id":"036e12f9db5b7590","type":"catch","z":"36690f145d5af6ca","name":"","scope":["a14424b157213fb4"],"uncaught":false,"x":803,"y":1124,"wires":[["256142c98e44b3d0"]]},{"id":"256142c98e44b3d0","type":"debug","z":"36690f145d5af6ca","name":"msg is bad","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"errors","targetType":"msg","statusVal":"","statusType":"auto","x":1064,"y":1125,"wires":[]},{"id":"62a6aee706773c33","type":"function","z":"36690f145d5af6ca","name":"invalid data","func":"msg.email = {\n cc: \"someemail\",\n to: \"asdsa\",\n}\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":557,"y":1148,"wires":[["a14424b157213fb4"]]}]