1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

JSON node: finalize JSON Schema validation

This commit is contained in:
Nathanaël Lécaudé 2018-06-30 16:19:39 -07:00
parent c39e2ffd56
commit 905f89b0f5
4 changed files with 102 additions and 19 deletions

View File

@ -697,7 +697,9 @@
"errors": { "errors": {
"dropped-object": "Ignored non-object payload", "dropped-object": "Ignored non-object payload",
"dropped": "Ignored unsupported payload type", "dropped": "Ignored unsupported payload type",
"dropped-error": "Failed to convert payload" "dropped-error": "Failed to convert payload",
"schema-error": "JSON Schema error",
"schema-error-compile": "JSON Schema error: failed to compile schema"
}, },
"label": { "label": {
"o2j": "Object to JSON options", "o2j": "Object to JSON options",

View File

@ -31,6 +31,8 @@
<dl class="message-properties"> <dl class="message-properties">
<dt>payload<span class="property-type">object | string</span></dt> <dt>payload<span class="property-type">object | string</span></dt>
<dd>A JavaScript object or JSON string.</dd> <dd>A JavaScript object or JSON string.</dd>
<dt>schema<span class="property-type">object</span></dt>
<dd>An optional JSON Schema object to validate the payload against.</dd>
</dl> </dl>
<h3>Outputs</h3> <h3>Outputs</h3>
<dl class="message-properties"> <dl class="message-properties">
@ -41,6 +43,9 @@
<li>If the input is a JavaScript object it creates a JSON string. The string can optionally be well-formatted.</li> <li>If the input is a JavaScript object it creates a JSON string. The string can optionally be well-formatted.</li>
</ul> </ul>
</dd> </dd>
<dt>schemaError<span class="property-type">array</span></dt>
<dd>If JSON schema validation fails, the catch node will have a <code>schemaError</code> property
containing an array of errors.</dd>
</dl> </dl>
<h3>Details</h3> <h3>Details</h3>
<p>By default, the node operates on <code>msg.payload</code>, but can be configured <p>By default, the node operates on <code>msg.payload</code>, but can be configured

View File

@ -32,24 +32,19 @@ module.exports = function(RED) {
this.on("input", function(msg) { this.on("input", function(msg) {
var validate = false; var validate = false;
if (msg.schema) { if (msg.schema) {
if (typeof msg.schema === "object") { // If input schema is different, re-compile it
// If input schema is different, re-compile it if (JSON.stringify(this.schema) != JSON.stringify(msg.schema)) {
if (JSON.stringify(this.schema) != JSON.stringify(msg.schema)) { try {
node.warn('Schema different, compiling'); this.compiledSchema = ajv.compile(msg.schema);
try { this.schema = msg.schema;
this.compiledSchema = ajv.compile(msg.schema); } catch(e) {
this.schema = msg.schema; this.schema = null;
} catch(e) { this.compiledSchema = null;
this.schema = null; node.error(RED._("json.errors.schema-error-compile"), msg);
this.compiledSchema = null; return;
node.error("JSON Schema error: failed to compile schema", msg);
return;
}
} }
validate = true;
} else {
node.warn("Schema present but not an object, ignoring schema");
} }
validate = true;
} }
var value = RED.util.getMessageProperty(msg,node.property); var value = RED.util.getMessageProperty(msg,node.property);
if (value !== undefined) { if (value !== undefined) {
@ -62,7 +57,7 @@ module.exports = function(RED) {
node.send(msg); node.send(msg);
} else { } else {
msg.schemaError = this.compiledSchema.errors; msg.schemaError = this.compiledSchema.errors;
node.error(`JSON Schema error: ${ajv.errorsText(this.compiledSchema.errors)}`, msg); node.error(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`, msg);
} }
} else { } else {
node.send(msg); node.send(msg);
@ -83,7 +78,7 @@ module.exports = function(RED) {
node.send(msg); node.send(msg);
} else { } else {
msg.schemaError = this.compiledSchema.errors; msg.schemaError = this.compiledSchema.errors;
node.error(`JSON Schema error: ${ajv.errorsText(this.compiledSchema.errors)}`, msg); node.error(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`, msg);
} }
} else { } else {
RED.util.setMessageProperty(msg,node.property,JSON.stringify(value,null,node.indent)); RED.util.setMessageProperty(msg,node.property,JSON.stringify(value,null,node.indent));

View File

@ -247,4 +247,85 @@ describe('JSON node', function() {
}); });
}); });
}); });
it('should pass an object if provided a valid JSON string and schema', function(done) {
var flow = [{id:"jn1",type:"json",wires:[["jn2"]]},
{id:"jn2", type:"helper"}];
helper.load(jsonNode, flow, function() {
var jn1 = helper.getNode("jn1");
var jn2 = helper.getNode("jn2");
jn2.on("input", function(msg) {
should.equal(msg.payload.number, 3);
should.equal(msg.payload.string, "allo");
done();
});
var jsonString = '{"number": 3, "string": "allo"}';
var schema = {title: "testSchema", type: "object", properties: {number: {type: "number"}, string: {type: "string" }}};
jn1.receive({payload:jsonString, schema:schema});
});
});
it('should pass a string if provided a valid object and schema', function(done) {
var flow = [{id:"jn1",type:"json",wires:[["jn2"]]},
{id:"jn2", type:"helper"}];
helper.load(jsonNode, flow, function() {
var jn1 = helper.getNode("jn1");
var jn2 = helper.getNode("jn2");
jn2.on("input", function(msg) {
should.equal(msg.payload, '{"number":3,"string":"allo"}');
done();
});
var obj = {"number": 3, "string": "allo"};
var schema = {title: "testSchema", type: "object", properties: {number: {type: "number"}, string: {type: "string" }}};
jn1.receive({payload:obj, schema:schema});
});
});
it('should log an error if passed an invalid object and valid schema', function(done) {
var flow = [{id:"jn1",type:"json",wires:[["jn2"]]},
{id:"jn2", type:"helper"}];
helper.load(jsonNode, flow, function() {
try {
var jn1 = helper.getNode("jn1");
var jn2 = helper.getNode("jn2");
var schema = {title: "testSchema", type: "object", properties: {number: {type: "number"}, string: {type: "string" }}};
var obj = {"number": "foo", "string": 3};
jn1.receive({payload:obj, schema:schema});
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "json";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.should.equal("json.errors.schema-error: data.number should be number, data.string should be string");
logEvents[0][0].should.have.a.property('level',helper.log().ERROR);
done();
} catch(err) {
done(err);
}
});
});
it('should log an error if passed a valid object and invalid schema', function(done) {
var flow = [{id:"jn1",type:"json",wires:[["jn2"]]},
{id:"jn2", type:"helper"}];
helper.load(jsonNode, flow, function() {
try {
var jn1 = helper.getNode("jn1");
var jn2 = helper.getNode("jn2");
var schema = "garbage";
var obj = {"number": "foo", "string": 3};
jn1.receive({payload:obj, schema:schema});
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "json";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.should.equal("json.errors.schema-error-compile");
logEvents[0][0].should.have.a.property('level',helper.log().ERROR);
done();
} catch(err) {
done(err);
}
});
});
}); });