mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
JSON node: finalize JSON Schema validation
This commit is contained in:
@@ -697,7 +697,9 @@
|
||||
"errors": {
|
||||
"dropped-object": "Ignored non-object payload",
|
||||
"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": {
|
||||
"o2j": "Object to JSON options",
|
||||
|
@@ -31,6 +31,8 @@
|
||||
<dl class="message-properties">
|
||||
<dt>payload<span class="property-type">object | string</span></dt>
|
||||
<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>
|
||||
<h3>Outputs</h3>
|
||||
<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>
|
||||
</ul>
|
||||
</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>
|
||||
<h3>Details</h3>
|
||||
<p>By default, the node operates on <code>msg.payload</code>, but can be configured
|
||||
|
@@ -32,24 +32,19 @@ module.exports = function(RED) {
|
||||
this.on("input", function(msg) {
|
||||
var validate = false;
|
||||
if (msg.schema) {
|
||||
if (typeof msg.schema === "object") {
|
||||
// If input schema is different, re-compile it
|
||||
if (JSON.stringify(this.schema) != JSON.stringify(msg.schema)) {
|
||||
node.warn('Schema different, compiling');
|
||||
try {
|
||||
this.compiledSchema = ajv.compile(msg.schema);
|
||||
this.schema = msg.schema;
|
||||
} catch(e) {
|
||||
this.schema = null;
|
||||
this.compiledSchema = null;
|
||||
node.error("JSON Schema error: failed to compile schema", msg);
|
||||
return;
|
||||
}
|
||||
// If input schema is different, re-compile it
|
||||
if (JSON.stringify(this.schema) != JSON.stringify(msg.schema)) {
|
||||
try {
|
||||
this.compiledSchema = ajv.compile(msg.schema);
|
||||
this.schema = msg.schema;
|
||||
} catch(e) {
|
||||
this.schema = null;
|
||||
this.compiledSchema = null;
|
||||
node.error(RED._("json.errors.schema-error-compile"), 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);
|
||||
if (value !== undefined) {
|
||||
@@ -62,7 +57,7 @@ module.exports = function(RED) {
|
||||
node.send(msg);
|
||||
} else {
|
||||
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 {
|
||||
node.send(msg);
|
||||
@@ -83,7 +78,7 @@ module.exports = function(RED) {
|
||||
node.send(msg);
|
||||
} else {
|
||||
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 {
|
||||
RED.util.setMessageProperty(msg,node.property,JSON.stringify(value,null,node.indent));
|
||||
|
Reference in New Issue
Block a user