How to check *types* with `if` in `function` node

Now it should be ok...

I went through and formatted the lines as I know.

Aren't there } missing in these places?

Yes, there are.
I've already corrected the code in the post above... for your reference.

Sorry.

I'm tripping over the replies.

I'll copy/paste the new version.

I hope I am not caught up with things.

Please, I am asking because I don't know, so don't get upset.

I get the first and second line.

But this magic in the third line.

${a} and ${type}

Oh.... Ok.
So rather than complicating the line - as I would write it to be:
node.warn("variable " + a + " is " + type);
you've written it as you did.
The ${a} and ${type} are .... *pointers (?) to the contents of the variables and so prints their contents in (between) the ` characters.
Why did you use those and not the " or ' characters?
Are the advantages to using them?

The backticks are a relatively new feature in Javascript to simplify concatenating strings and variables.
As you already realised

node.warn(`typeof ${a} is ${type}`)

is the same as
node.warn("typeof " + a + " is " + type)

For me yes.
I can never remember if I need to use + or & or , to concatenate strings and values in a node.warn statement.
It's also familiar, now deprecated, syntax for a vaguely similar construction in Unix shell scripts.

I only recently read about the syntax ("template literals") in a page linked to by someone in your thread "Function nodes and 'let' for variables."

1 Like

Sorry for the long delay in posting and not following things up.

I just discovered a slight problem with the node/code.

The idea is that the code checks for the required structure:
rgb,0,1,2,3 (For example.)

But if I send rgb,o,1,2,3 it also passes.

My initial test was to check missing values.
And now - ironically - even that doesn't seem to be working.

Code:

[{"id":"5204cbf368ea0df2","type":"function","z":"d188b95f33e5f7e4","name":"message format check","func":"//  Split the payload.\nconst check = msg.payload.split(\",\");\nlet error = false;\n\nfor (let i = 0, l = check.length; i < l; i++) {\n    const c = check[i];\n    //node.warn(\"Checking \" + c);\n    switch (i) {\n        case 0: {\n            if (c !== \"rgb\") {\n                error = true;\n                break;\n            }\n        }\n        case 1: {\n            if (\"number\" !== typeof c && c < 0 && c > 8) {   // you're sure it's '8' , not '7' ? \n                error = true;\n                break;\n            }\n        }\n        default: {\n            if (\"number\" !== typeof c && c < 0 && c > 255) {\n                error = true;\n                break;\n            }\n        }\n    }\n}\n\nif (error) {\n    //\n    node.warn(\"There's a problem...\");\n    return [null,msg];\n}\n\nreturn msg;","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1985,"y":580,"wires":[["06770fdd22d032f8"],["ee97d5a02007f898"]],"outputLabels":["Good message","Bad message"],"l":false},{"id":"26c46db02b5ee25f","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,0,1,2,3","payloadType":"str","x":1820,"y":510,"wires":[["5204cbf368ea0df2"]]},{"id":"5cc9b6bb2f0f5926","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,o,1,2,3","payloadType":"str","x":1820,"y":540,"wires":[["5204cbf368ea0df2"]]},{"id":"482677f5d7bb2025","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,0,i,2,3","payloadType":"str","x":1820,"y":570,"wires":[["5204cbf368ea0df2"]]},{"id":"185d0f8706cb36b3","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,,1,Z,3","payloadType":"str","x":1820,"y":600,"wires":[["5204cbf368ea0df2"]]},{"id":"d95a1c38f04c4f1b","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,0,1,2,E","payloadType":"str","x":1820,"y":630,"wires":[["5204cbf368ea0df2"]]},{"id":"6cbf789e3fcc449c","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,,1,2,3","payloadType":"str","x":1820,"y":660,"wires":[["5204cbf368ea0df2"]]},{"id":"d61fdb93f7be6775","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,0,,2,3","payloadType":"str","x":1820,"y":690,"wires":[["5204cbf368ea0df2"]]},{"id":"a47e39a43ffcf7d3","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,0,1,,3","payloadType":"str","x":1820,"y":720,"wires":[["5204cbf368ea0df2"]]},{"id":"e3cffd78fb8efec8","type":"inject","z":"d188b95f33e5f7e4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"rgb,0,1,2,","payloadType":"str","x":1820,"y":750,"wires":[["5204cbf368ea0df2"]]},{"id":"06770fdd22d032f8","type":"debug","z":"d188b95f33e5f7e4","name":"GOOD","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2120,"y":540,"wires":[]},{"id":"ee97d5a02007f898","type":"debug","z":"d188b95f33e5f7e4","name":"WRONG","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2130,"y":610,"wires":[]}]

I'll do some more testing, but if I don't ask..... :wink:

In your function:

Try this instead

//  Split the payload.
const check = msg.payload.split(",");
let error = false;

for (let i = 0, l = check.length; i < l; i++) {
    const c = check[i];
    switch (i) {
        case 0: {
            if (c !== "rgb") {
                node.warn(`Error in part ${i}: ${c} is invalid`)
                error = true;
            }
            break;
        }
        case 1: {
            let digit = parseInt(c) // gives either an integer or NaN
            if (!(digit >= 0 && digit <= 8)) {   // you're sure it's '8' , not '7' ? 
                node.warn(`Error in part ${i}: ${c} is invalid`)
                error = true;  
            }
            break;
        }
        default: {
            let digit = parseInt(c)
            if (!(digit >= 0 && digit <= 255)) {
                node.warn(`Error in part ${i}: ${c} is invalid`)
                error = true;
            }
            break;
        }
    }
}

if (error) {
    //
    node.warn("There's a problem...");
    return [null,msg];
}

return msg;
1 Like

Thanks for that.

I just copied the code posted in reply 12.

I am not sure if it was simply the fact that I put the node/code in and it frightened the errors away for a while. :wink:

But they regrouped and came back with a bit of vengeance.

I was a bit suspicious the original code wouldn't work, but as it seemed to fix the problem.

Anyway, falling forwards I see what you posted makes more sense.
I'll now go back and shake the tree again and see what happens. :slight_smile:

Appreciated.