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

Add common validator for typedInput fields

Closes #1104
This commit is contained in:
Nick O'Leary 2017-01-06 22:20:09 +00:00
parent 73a41707e5
commit 4b83d8160f
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 18 additions and 17 deletions

View File

@ -15,5 +15,21 @@
**/ **/
RED.validators = { RED.validators = {
number: function(){return function(v) { return v!=='' && !isNaN(v);}}, 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;
}}
}; };

View File

@ -175,22 +175,7 @@
defaults: { defaults: {
name: {value:""}, name: {value:""},
topic: {value:""}, topic: {value:""},
payload: {value:"", validate:function(v) { payload: {value:"", validate: RED.validators.typedInput("payloadType")},
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;
}},
payloadType: {value:"date"}, payloadType: {value:"date"},
repeat: {value:""}, repeat: {value:""},
crontab: {value:""}, crontab: {value:""},