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

let setMessageProperty return success flag

so calling node can warn if operation tries to overwrite primitive type
This commit is contained in:
Dave Conway-Jones 2020-01-26 18:20:25 +00:00
parent 9d4238e5cc
commit d5d9ac5c76
No known key found for this signature in database
GPG Key ID: 302A6725C594817F
3 changed files with 17 additions and 3 deletions

View File

@ -215,7 +215,9 @@ module.exports = function(RED) {
if (rule.t === 'delete') { if (rule.t === 'delete') {
RED.util.setMessageProperty(msg,property,undefined); RED.util.setMessageProperty(msg,property,undefined);
} else if (rule.t === 'set') { } else if (rule.t === 'set') {
RED.util.setMessageProperty(msg,property,value); if (!RED.util.setMessageProperty(msg,property,value)) {
node.warn(RED._("change.errors.no-override",{prop:property}));
}
} else if (rule.t === 'change') { } else if (rule.t === 'change') {
current = RED.util.getMessageProperty(msg,property); current = RED.util.getMessageProperty(msg,property);
if (typeof current === 'string') { if (typeof current === 'string') {

View File

@ -657,7 +657,8 @@
"errors": { "errors": {
"invalid-from": "Invalid 'from' property: __error__", "invalid-from": "Invalid 'from' property: __error__",
"invalid-json": "Invalid 'to' JSON property", "invalid-json": "Invalid 'to' JSON property",
"invalid-expr": "Invalid JSONata expression: __error__" "invalid-expr": "Invalid JSONata expression: __error__",
"no-override": "Can't overwrite primitive type with object __prop__."
} }
}, },
"range": { "range": {

View File

@ -368,10 +368,15 @@ function setObjectProperty(msg,prop,value,createMissing) {
var length = msgPropParts.length; var length = msgPropParts.length;
var obj = msg; var obj = msg;
var key; var key;
var flag = true;
for (var i=0;i<length-1;i++) { for (var i=0;i<length-1;i++) {
key = msgPropParts[i]; key = msgPropParts[i];
if (typeof key === 'string' || (typeof key === 'number' && !Array.isArray(obj))) { if (typeof key === 'string' || (typeof key === 'number' && !Array.isArray(obj))) {
if (obj.hasOwnProperty(key)) { if (obj.hasOwnProperty(key)) {
if (length > 1 && ((typeof obj[key] !== "object" && typeof obj[key] !== "function") || obj[key] === null)) {
//console.log("Can't override primitive type with object.");
flag = false;
}
obj = obj[key]; obj = obj[key];
} else if (createMissing) { } else if (createMissing) {
if (typeof msgPropParts[i+1] === 'string') { if (typeof msgPropParts[i+1] === 'string') {
@ -409,8 +414,14 @@ function setObjectProperty(msg,prop,value,createMissing) {
delete obj[key] delete obj[key]
} }
} else { } else {
obj[key] = value; if (typeof obj === "object" && obj !== null) {
obj[key] = value;
} else {
//console.log("Can't override primitive type with object.");
flag = false;
}
} }
return flag;
} }
/*! /*!