diff --git a/nodes/core/core/20-inject.html b/nodes/core/core/20-inject.html index 80fbf4c85..309061df3 100644 --- a/nodes/core/core/20-inject.html +++ b/nodes/core/core/20-inject.html @@ -172,7 +172,22 @@ defaults: { name: {value:""}, topic: {value:""}, - payload: {value:""}, + payload: {value:"", validate:function(v) { + var ptype = $("#node-input-payloadType").val() || this.payloadType; + if (ptype === 'json') { + try { + JSON.parse(v); + return true; + } catch(err) { + return false; + } + } else if (ptype === 'flow' || ptype === 'global' ) { + return /^[a-zA-Z_][a-zA-Z0-9_]*(\.[a-zA-Z_][a-zA-Z0-9_]+)*/i.test(v); + } else if (ptype === 'num') { + return /^[+-]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)?$/.test(v); + } + return true; + }}, payloadType: {value:"date"}, repeat: {value:""}, crontab: {value:""}, diff --git a/nodes/core/core/20-inject.js b/nodes/core/core/20-inject.js index 05c863cd6..1e1744dbb 100644 --- a/nodes/core/core/20-inject.js +++ b/nodes/core/core/20-inject.js @@ -50,18 +50,22 @@ module.exports = function(RED) { } this.on("input",function(msg) { - var msg = {topic:this.topic}; - if ( (this.payloadType == null && this.payload === "") || this.payloadType === "date") { - msg.payload = Date.now(); - } else if (this.payloadType == null) { - msg.payload = this.payload; - } else if (this.payloadType == 'none') { - msg.payload = ""; - } else { - msg.payload = RED.util.evaluateNodeProperty(this.payload,this.payloadType,this,msg); + try { + msg = {topic:this.topic}; + if ( (this.payloadType == null && this.payload === "") || this.payloadType === "date") { + msg.payload = Date.now(); + } else if (this.payloadType == null) { + msg.payload = this.payload; + } else if (this.payloadType == 'none') { + msg.payload = ""; + } else { + msg.payload = RED.util.evaluateNodeProperty(this.payload,this.payloadType,this,msg); + } + this.send(msg); + msg = null; + } catch(err) { + this.error(err,msg); } - this.send(msg); - msg = null; }); }