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

new-style callback function (json node)

This commit is contained in:
Kunihiko Toumura 2019-11-29 16:51:23 +09:00
parent d8eb80b72e
commit a19dab0dc9

View File

@ -31,7 +31,7 @@ module.exports = function(RED) {
var node = this;
this.on("input", function(msg) {
this.on("input", function(msg,send,done) {
var validate = false;
if (msg.schema) {
// If input schema is different, re-compile it
@ -42,7 +42,7 @@ module.exports = function(RED) {
} catch(e) {
this.schema = null;
this.compiledSchema = null;
node.error(RED._("json.errors.schema-error-compile"), msg);
done(RED._("json.errors.schema-error-compile"));
return;
}
}
@ -57,28 +57,32 @@ module.exports = function(RED) {
if (validate) {
if (this.compiledSchema(msg[node.property])) {
delete msg.schema;
node.send(msg);
send(msg);
done();
} else {
msg.schemaError = this.compiledSchema.errors;
node.error(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`, msg);
done(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`);
}
} else {
node.send(msg);
send(msg);
done();
}
}
catch(e) { node.error(e.message,msg); }
catch(e) { done(e.message); }
} else {
// If node.action is str and value is str
if (validate) {
if (this.compiledSchema(JSON.parse(msg[node.property]))) {
delete msg.schema;
node.send(msg);
send(msg);
done();
} else {
msg.schemaError = this.compiledSchema.errors;
node.error(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`, msg);
done(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`);
}
} else {
node.send(msg);
send(msg);
done();
}
}
}
@ -90,37 +94,41 @@ module.exports = function(RED) {
if (this.compiledSchema(value)) {
RED.util.setMessageProperty(msg,node.property,JSON.stringify(value,null,node.indent));
delete msg.schema;
node.send(msg);
send(msg);
done();
} else {
msg.schemaError = this.compiledSchema.errors;
node.error(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`, msg);
done(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`);
}
} else {
RED.util.setMessageProperty(msg,node.property,JSON.stringify(value,null,node.indent));
node.send(msg);
send(msg);
done();
}
}
catch(e) { node.error(RED._("json.errors.dropped-error")); }
catch(e) { done(RED._("json.errors.dropped-error")); }
}
else { node.warn(RED._("json.errors.dropped-object")); }
else { node.warn(RED._("json.errors.dropped-object")); done(); }
} else {
// If node.action is obj and value is object
if (validate) {
if (this.compiledSchema(value)) {
delete msg.schema;
node.send(msg);
send(msg);
done();
} else {
msg.schemaError = this.compiledSchema.errors;
node.error(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`, msg);
done(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`);
}
} else {
node.send(msg);
send(msg);
done();
}
}
}
else { node.warn(RED._("json.errors.dropped")); }
else { node.warn(RED._("json.errors.dropped")); done(); }
}
else { node.send(msg); } // If no property - just pass it on.
else { send(msg); done(); } // If no property - just pass it on.
});
}
RED.nodes.registerType("json",JSONNode);