From 4b83d8160fab768aa728da7dd9d7cb150066105e Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Fri, 6 Jan 2017 22:20:09 +0000 Subject: [PATCH] Add common validator for typedInput fields Closes #1104 --- editor/js/validators.js | 18 +++++++++++++++++- nodes/core/core/20-inject.html | 17 +---------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/editor/js/validators.js b/editor/js/validators.js index 5e740973b..821593b79 100644 --- a/editor/js/validators.js +++ b/editor/js/validators.js @@ -15,5 +15,21 @@ **/ RED.validators = { number: function(){return function(v) { return v!=='' && !isNaN(v);}}, - regex: function(re){return function(v) { return re.test(v);}} + regex: function(re){return function(v) { return re.test(v);}}, + typedInput: function(ptypeName) { return function(v) { + var ptype = $("#node-input-"+ptypeName).val() || this[ptypeName]; + if (ptype === 'json') { + try { + JSON.parse(v); + return true; + } catch(err) { + return false; + } + } else if (ptype === 'flow' || ptype === 'global' ) { + return RED.utils.validatePropertyExpression(v); + } else if (ptype === 'num') { + return /^[+-]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)?$/.test(v); + } + return true; + }} }; diff --git a/nodes/core/core/20-inject.html b/nodes/core/core/20-inject.html index b7e0f996c..f820bcee6 100644 --- a/nodes/core/core/20-inject.html +++ b/nodes/core/core/20-inject.html @@ -175,22 +175,7 @@ defaults: { name: {value:""}, topic: {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 RED.utils.validatePropertyExpression(v); - } else if (ptype === 'num') { - return /^[+-]?[0-9]*\.?[0-9]*([eE][-+]?[0-9]+)?$/.test(v); - } - return true; - }}, + payload: {value:"", validate: RED.validators.typedInput("payloadType")}, payloadType: {value:"date"}, repeat: {value:""}, crontab: {value:""},