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

Add property validate to Change node rule set

Closes #2911
This commit is contained in:
Nick O'Leary 2021-04-06 17:25:12 +01:00
parent 400141b093
commit e3b70b10d1
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9

View File

@ -18,12 +18,43 @@
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
(function() {
function validateProperty(v,vt) {
if (/msg|flow|global/.test(vt)) {
if (!RED.utils.validatePropertyExpression(v)) {
return false;
}
} else if (v[i].vt === "jsonata") {
try{jsonata(v);}catch(e){return false;}
} else if ([i].vt === "json") {
try{JSON.parse(v);}catch(e){return false;}
}
}
RED.nodes.registerType('change', { RED.nodes.registerType('change', {
color: "#E2D96E", color: "#E2D96E",
category: 'function', category: 'function',
defaults: { defaults: {
name: {value:""}, name: {value:""},
rules:{value:[{t:"set",p:"payload",pt:"msg",to:"",tot:"str"}]}, rules:{value:[{t:"set",p:"payload",pt:"msg",to:"",tot:"str"}],validate: function(rules) {
if (!rules || rules.length === 0) { return true }
for (var i=0;i<rules.length;i++) {
var r = rules[i];
if (r.t === 'set') {
if (!validateProperty(r.p,r.pt) || !validateProperty(r.to,r.tot)) {
return false;
}
} else if (r.t === 'change') {
if (!validateProperty(r.p,r.pt) || !validateProperty(r.from,r.fromt) || !validateProperty(r.to,r.tot)) {
return false;
}
} else if (r.t === 'move') {
if (!validateProperty(r.p,r.pt)) {
return false;
}
}
}
return true;
}},
// legacy // legacy
action: {value:""}, action: {value:""},
property: {value:""}, property: {value:""},
@ -323,4 +354,5 @@
$("#node-input-rule-container").editableList('height',height); $("#node-input-rule-container").editableList('height',height);
} }
}); });
})();
</script> </script>