mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Add type checks to switch node options (#1714)
* Add type checks to switch node options * add isType to messages.json
This commit is contained in:
parent
60ff8660de
commit
4a4513a746
@ -568,6 +568,7 @@
|
|||||||
"false":"is false",
|
"false":"is false",
|
||||||
"null":"is null",
|
"null":"is null",
|
||||||
"nnull":"is not null",
|
"nnull":"is not null",
|
||||||
|
"istype":"is of type",
|
||||||
"head":"head",
|
"head":"head",
|
||||||
"tail":"tail",
|
"tail":"tail",
|
||||||
"index":"index between",
|
"index":"index between",
|
||||||
|
@ -85,6 +85,7 @@
|
|||||||
{v:"false",t:"switch.rules.false",kind:'V'},
|
{v:"false",t:"switch.rules.false",kind:'V'},
|
||||||
{v:"null",t:"switch.rules.null",kind:'V'},
|
{v:"null",t:"switch.rules.null",kind:'V'},
|
||||||
{v:"nnull",t:"switch.rules.nnull",kind:'V'},
|
{v:"nnull",t:"switch.rules.nnull",kind:'V'},
|
||||||
|
{v:"istype",t:"switch.rules.istype",kind:'V'},
|
||||||
{v:"head",t:"switch.rules.head",kind:'S'},
|
{v:"head",t:"switch.rules.head",kind:'S'},
|
||||||
{v:"index",t:"switch.rules.index",kind:'S'},
|
{v:"index",t:"switch.rules.index",kind:'S'},
|
||||||
{v:"tail",t:"switch.rules.tail",kind:'S'},
|
{v:"tail",t:"switch.rules.tail",kind:'S'},
|
||||||
@ -160,6 +161,7 @@
|
|||||||
var selectField = rule.find("select");
|
var selectField = rule.find("select");
|
||||||
var type = selectField.val()||"";
|
var type = selectField.val()||"";
|
||||||
var valueField = rule.find(".node-input-rule-value");
|
var valueField = rule.find(".node-input-rule-value");
|
||||||
|
var typeField = rule.find(".node-input-rule-type-value");
|
||||||
var numField = rule.find(".node-input-rule-num-value");
|
var numField = rule.find(".node-input-rule-num-value");
|
||||||
var expField = rule.find(".node-input-rule-exp-value");
|
var expField = rule.find(".node-input-rule-exp-value");
|
||||||
var btwnField1 = rule.find(".node-input-rule-btwn-value");
|
var btwnField1 = rule.find(".node-input-rule-btwn-value");
|
||||||
@ -180,6 +182,8 @@
|
|||||||
numField.typedInput("width",(newWidth-selectWidth-70));
|
numField.typedInput("width",(newWidth-selectWidth-70));
|
||||||
} else if (type === "jsonata_exp") {
|
} else if (type === "jsonata_exp") {
|
||||||
expField.typedInput("width",(newWidth-selectWidth-70));
|
expField.typedInput("width",(newWidth-selectWidth-70));
|
||||||
|
} else if (type === "istype") {
|
||||||
|
typeField.typedInput("width",(newWidth-selectWidth-70));
|
||||||
} else {
|
} else {
|
||||||
if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
|
if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
|
||||||
// valueField.hide();
|
// valueField.hide();
|
||||||
@ -232,6 +236,18 @@
|
|||||||
var btwnValueField = $('<input/>',{class:"node-input-rule-btwn-value",type:"text",style:"margin-left: 5px;"}).appendTo(row).typedInput({default:'num',types:['msg','flow','global','str','num','jsonata',previousValueType]});
|
var btwnValueField = $('<input/>',{class:"node-input-rule-btwn-value",type:"text",style:"margin-left: 5px;"}).appendTo(row).typedInput({default:'num',types:['msg','flow','global','str','num','jsonata',previousValueType]});
|
||||||
var btwnAndLabel = $('<span/>',{class:"node-input-rule-btwn-label"}).text(" "+andLabel+" ").appendTo(row3);
|
var btwnAndLabel = $('<span/>',{class:"node-input-rule-btwn-label"}).text(" "+andLabel+" ").appendTo(row3);
|
||||||
var btwnValue2Field = $('<input/>',{class:"node-input-rule-btwn-value2",type:"text",style:"margin-left:2px;"}).appendTo(row3).typedInput({default:'num',types:['msg','flow','global','str','num','jsonata',previousValueType]});
|
var btwnValue2Field = $('<input/>',{class:"node-input-rule-btwn-value2",type:"text",style:"margin-left:2px;"}).appendTo(row3).typedInput({default:'num',types:['msg','flow','global','str','num','jsonata',previousValueType]});
|
||||||
|
var typeValueField = $('<input/>',{class:"node-input-rule-type-value",type:"text",style:"margin-left: 5px;"}).appendTo(row)
|
||||||
|
.typedInput({default:'string',types:[
|
||||||
|
{value:"string",label:"string",hasValue:false},
|
||||||
|
{value:"number",label:"number",hasValue:false},
|
||||||
|
{value:"boolean",label:"boolean",hasValue:false},
|
||||||
|
{value:"array",label:"array",hasValue:false},
|
||||||
|
{value:"buffer",label:"buffer",hasValue:false},
|
||||||
|
{value:"object",label:"object",hasValue:false},
|
||||||
|
{value:"json",label:"JSON string",hasValue:false},
|
||||||
|
{value:"undefined",label:"undefined",hasValue:false},
|
||||||
|
{value:"null",label:"null",hasValue:false}
|
||||||
|
]});
|
||||||
var finalspan = $('<span/>',{style:"float: right;margin-top: 6px;"}).appendTo(row);
|
var finalspan = $('<span/>',{style:"float: right;margin-top: 6px;"}).appendTo(row);
|
||||||
finalspan.append(' → <span class="node-input-rule-index">'+(i+1)+'</span> ');
|
finalspan.append(' → <span class="node-input-rule-index">'+(i+1)+'</span> ');
|
||||||
var caseSensitive = $('<input/>',{id:"node-input-rule-case-"+i,class:"node-input-rule-case",type:"checkbox",style:"width:auto;vertical-align:top"}).appendTo(row2);
|
var caseSensitive = $('<input/>',{id:"node-input-rule-case-"+i,class:"node-input-rule-case",type:"checkbox",style:"width:auto;vertical-align:top"}).appendTo(row2);
|
||||||
@ -243,26 +259,39 @@
|
|||||||
valueField.typedInput('hide');
|
valueField.typedInput('hide');
|
||||||
expValueField.typedInput('hide');
|
expValueField.typedInput('hide');
|
||||||
numValueField.typedInput('hide');
|
numValueField.typedInput('hide');
|
||||||
|
typeValueField.typedInput('hide');
|
||||||
btwnValueField.typedInput('show');
|
btwnValueField.typedInput('show');
|
||||||
} else if ((type === "head") || (type === "tail")) {
|
} else if ((type === "head") || (type === "tail")) {
|
||||||
btwnValueField.typedInput('hide');
|
btwnValueField.typedInput('hide');
|
||||||
btwnValue2Field.typedInput('hide');
|
btwnValue2Field.typedInput('hide');
|
||||||
expValueField.typedInput('hide');
|
expValueField.typedInput('hide');
|
||||||
numValueField.typedInput('show');
|
numValueField.typedInput('show');
|
||||||
|
typeValueField.typedInput('hide');
|
||||||
valueField.typedInput('hide');
|
valueField.typedInput('hide');
|
||||||
} else if (type === "jsonata_exp") {
|
} else if (type === "jsonata_exp") {
|
||||||
btwnValueField.typedInput('hide');
|
btwnValueField.typedInput('hide');
|
||||||
btwnValue2Field.typedInput('hide');
|
btwnValue2Field.typedInput('hide');
|
||||||
expValueField.typedInput('show');
|
expValueField.typedInput('show');
|
||||||
numValueField.typedInput('hide');
|
numValueField.typedInput('hide');
|
||||||
|
typeValueField.typedInput('hide');
|
||||||
valueField.typedInput('hide');
|
valueField.typedInput('hide');
|
||||||
} else {
|
} else {
|
||||||
btwnValueField.typedInput('hide');
|
btwnValueField.typedInput('hide');
|
||||||
expValueField.typedInput('hide');
|
expValueField.typedInput('hide');
|
||||||
numValueField.typedInput('hide');
|
numValueField.typedInput('hide');
|
||||||
|
typeValueField.typedInput('hide');
|
||||||
|
valueField.typedInput('hide');
|
||||||
if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
|
if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
|
||||||
valueField.typedInput('hide');
|
valueField.typedInput('hide');
|
||||||
} else {
|
typeValueField.typedInput('hide');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if (type === "istype") {
|
||||||
|
valueField.typedInput('hide');
|
||||||
|
typeValueField.typedInput('show');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
typeValueField.typedInput('hide');
|
||||||
valueField.typedInput('show');
|
valueField.typedInput('show');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -287,6 +316,9 @@
|
|||||||
} else if ((rule.t === "head") || (rule.t === "tail")) {
|
} else if ((rule.t === "head") || (rule.t === "tail")) {
|
||||||
numValueField.typedInput('value',rule.v);
|
numValueField.typedInput('value',rule.v);
|
||||||
numValueField.typedInput('type',rule.vt||'num');
|
numValueField.typedInput('type',rule.vt||'num');
|
||||||
|
} else if (rule.t === "istype") {
|
||||||
|
typeValueField.typedInput('value',rule.vt);
|
||||||
|
typeValueField.typedInput('type',rule.vt);
|
||||||
} else if (rule.t === "jsonata_exp") {
|
} else if (rule.t === "jsonata_exp") {
|
||||||
expValueField.typedInput('value',rule.v);
|
expValueField.typedInput('value',rule.v);
|
||||||
expValueField.typedInput('type',rule.vt||'jsonata');
|
expValueField.typedInput('type',rule.vt||'jsonata');
|
||||||
@ -359,6 +391,9 @@
|
|||||||
} else if ((type === "head") || (type === "tail")) {
|
} else if ((type === "head") || (type === "tail")) {
|
||||||
r.v = rule.find(".node-input-rule-num-value").typedInput('value');
|
r.v = rule.find(".node-input-rule-num-value").typedInput('value');
|
||||||
r.vt = rule.find(".node-input-rule-num-value").typedInput('type');
|
r.vt = rule.find(".node-input-rule-num-value").typedInput('type');
|
||||||
|
} else if (type === "istype") {
|
||||||
|
r.v = rule.find(".node-input-rule-type-value").typedInput('type');
|
||||||
|
r.vt = rule.find(".node-input-rule-type-value").typedInput('type');
|
||||||
} else if (type === "jsonata_exp") {
|
} else if (type === "jsonata_exp") {
|
||||||
r.v = rule.find(".node-input-rule-exp-value").typedInput('value');
|
r.v = rule.find(".node-input-rule-exp-value").typedInput('value');
|
||||||
r.vt = rule.find(".node-input-rule-exp-value").typedInput('type');
|
r.vt = rule.find(".node-input-rule-exp-value").typedInput('type');
|
||||||
|
@ -31,6 +31,16 @@ module.exports = function(RED) {
|
|||||||
'false': function(a) { return a === false; },
|
'false': function(a) { return a === false; },
|
||||||
'null': function(a) { return (typeof a == "undefined" || a === null); },
|
'null': function(a) { return (typeof a == "undefined" || a === null); },
|
||||||
'nnull': function(a) { return (typeof a != "undefined" && a !== null); },
|
'nnull': function(a) { return (typeof a != "undefined" && a !== null); },
|
||||||
|
'istype': function(a, b) {
|
||||||
|
if (b === "array") { return Array.isArray(a); }
|
||||||
|
else if (b === "buffer") { return Buffer.isBuffer(a); }
|
||||||
|
else if (b === "json") {
|
||||||
|
try { JSON.parse(a); return true; } // or maybe ??? a !== null; }
|
||||||
|
catch(e) { return false;}
|
||||||
|
}
|
||||||
|
else if (b === "null") { return a === null; }
|
||||||
|
else { return typeof a === b && !Array.isArray(a) && !Buffer.isBuffer(a) && a !== null; }
|
||||||
|
},
|
||||||
'head': function(a, b, c, d, parts) {
|
'head': function(a, b, c, d, parts) {
|
||||||
var count = Number(b);
|
var count = Number(b);
|
||||||
return (parts.index < count);
|
return (parts.index < count);
|
||||||
@ -292,6 +302,10 @@ module.exports = function(RED) {
|
|||||||
node.error(RED._("switch.errors.invalid-expr",{error:err.message}));
|
node.error(RED._("switch.errors.invalid-expr",{error:err.message}));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
} else if (rule.vt === 'json') {
|
||||||
|
v1 = "json";
|
||||||
|
} else if (rule.vt === 'null') {
|
||||||
|
v1 = "null";
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
v1 = RED.util.evaluateNodeProperty(rule.v,rule.vt,node,msg);
|
v1 = RED.util.evaluateNodeProperty(rule.v,rule.vt,node,msg);
|
||||||
|
@ -103,7 +103,7 @@ describe('switch Node', function() {
|
|||||||
helperNode1.on("input", function(msg) {
|
helperNode1.on("input", function(msg) {
|
||||||
try {
|
try {
|
||||||
if (shouldReceive === true) {
|
if (shouldReceive === true) {
|
||||||
msg.payload.should.equal(sendPayload);
|
should.equal(msg.payload,sendPayload);
|
||||||
done();
|
done();
|
||||||
} else {
|
} else {
|
||||||
should.fail(null, null, "We should never get an input!");
|
should.fail(null, null, "We should never get an input!");
|
||||||
@ -168,8 +168,6 @@ describe('switch Node', function() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
it('should check if payload equals given value', function(done) {
|
it('should check if payload equals given value', function(done) {
|
||||||
genericSwitchTest("eq", "Hello", true, true, "Hello", done);
|
genericSwitchTest("eq", "Hello", true, true, "Hello", done);
|
||||||
});
|
});
|
||||||
@ -258,6 +256,43 @@ describe('switch Node', function() {
|
|||||||
genericSwitchTest("regex", "[abc]+", true, true, "abbabac", done);
|
genericSwitchTest("regex", "[abc]+", true, true, "abbabac", done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should check if payload if of type string ', function(done) {
|
||||||
|
genericSwitchTest("istype", "string", true, true, "Hello", done);
|
||||||
|
});
|
||||||
|
it('should check if payload if of type number ', function(done) {
|
||||||
|
genericSwitchTest("istype", "number", true, true, 999, done);
|
||||||
|
});
|
||||||
|
it('should check if payload if of type number 0', function(done) {
|
||||||
|
genericSwitchTest("istype", "number", true, true, 0, done);
|
||||||
|
});
|
||||||
|
it('should check if payload if of type boolean true', function(done) {
|
||||||
|
genericSwitchTest("istype", "boolean", true, true, true, done);
|
||||||
|
});
|
||||||
|
it('should check if payload if of type boolean false', function(done) {
|
||||||
|
genericSwitchTest("istype", "boolean", true, true, true, done);
|
||||||
|
});
|
||||||
|
it('should check if payload if of type array ', function(done) {
|
||||||
|
genericSwitchTest("istype", "array", true, true, [1,2,3,"a","b"], done);
|
||||||
|
});
|
||||||
|
it('should check if payload if of type buffer ', function(done) {
|
||||||
|
genericSwitchTest("istype", "buffer", true, true, Buffer.from("Hello"), done);
|
||||||
|
});
|
||||||
|
it('should check if payload if of type object ', function(done) {
|
||||||
|
genericSwitchTest("istype", "object", true, true, {a:1,b:"b",c:true}, done);
|
||||||
|
});
|
||||||
|
it('should check if payload if of type JSON string ', function(done) {
|
||||||
|
genericSwitchTest("istype", "json", true, true, JSON.stringify({a:1,b:"b",c:true}), done);
|
||||||
|
});
|
||||||
|
it('should check if payload if of type JSON string (and fail if not) ', function(done) {
|
||||||
|
genericSwitchTest("istype", "json", true, false, "Hello", done);
|
||||||
|
});
|
||||||
|
it('should check if payload if of type null', function(done) {
|
||||||
|
genericSwitchTest("istype", "null", true, true, null, done);
|
||||||
|
});
|
||||||
|
it('should check if payload if of type undefined', function(done) {
|
||||||
|
genericSwitchTest("istype", "undefined", true, true, undefined, done);
|
||||||
|
});
|
||||||
|
|
||||||
it('should match regex with ignore-case flag set true', function(done) {
|
it('should match regex with ignore-case flag set true', function(done) {
|
||||||
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"regex","v":"onetwothree","case":true}],checkall:true,outputs:1,wires:[["helperNode1"]]},
|
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"regex","v":"onetwothree","case":true}],checkall:true,outputs:1,wires:[["helperNode1"]]},
|
||||||
{id:"helperNode1", type:"helper", wires:[]}];
|
{id:"helperNode1", type:"helper", wires:[]}];
|
||||||
@ -433,7 +468,6 @@ describe('switch Node', function() {
|
|||||||
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"nnull"}],checkall:false,outputs:1,wires:[["helperNode1"]]},
|
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"nnull"}],checkall:false,outputs:1,wires:[["helperNode1"]]},
|
||||||
{id:"helperNode1", type:"helper", wires:[]}];
|
{id:"helperNode1", type:"helper", wires:[]}];
|
||||||
|
|
||||||
|
|
||||||
helper.load(switchNode, flow, function() {
|
helper.load(switchNode, flow, function() {
|
||||||
var switchNode1 = helper.getNode("switchNode1");
|
var switchNode1 = helper.getNode("switchNode1");
|
||||||
var helperNode1 = helper.getNode("helperNode1");
|
var helperNode1 = helper.getNode("helperNode1");
|
||||||
@ -787,5 +821,4 @@ describe('switch Node', function() {
|
|||||||
n1.receive({payload:1, parts:{index:0, count:4, id:222}});
|
n1.receive({payload:1, parts:{index:0, count:4, id:222}});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user