Validate input message in UI node

Hi folks,

When developing a non-UI node, input messages can easily be validated (and optionally skipped):

node.on("input", function(msg) {
    if(!Array.isArray(msg.payload)) {
        // Skip input message when payload doesn't contain an array
        return;
    }

    // Process the input message ...
});

How can I achieve something similar for UI nodes (without messing-up the internal UI node setup)? Have been looking at the already existing UI nodes, but couldn't find something alike...

Thanks !!
Bart

Not on laptop this sec. But I think its the before send function you want.. You can validate/reject something there before it gets sent to the ui

Hey Dave (@dceejay),

When I inject a message into my UI node, I arrive at server-side in the beforeEmit (not in the beforeSend which might be triggered before an output message is send??):

image

But I'm not sure if I can reject the input message somehow, i.e. not send it to the dashboard when invalid. Because when I look at the caller of that function, it doesn't seem like there is any check of the return value that aborts the sending process:

image

Anybody have a suggestion?

Do not asume that you can cover all possible situations and if data to be handled is not "your kind of data" make fallback to some default value and send it anyway. Keep things running, log warnings of incorrect data usage.

Apologies - yes I was thinking of beforeEmit. I was hoping returning null would work - but indeed looks like it would cause more bother. We can certainly add that capability but obviously the level of dashboard required cannot be assumed by your node. (for a while)

Hey @hotNipi, believe me that covering all possible situations is the last thing I want to achieve at this moment :wink:

But validating my input messages at server side has a lot of advantages:

  1. More efficient use of system resources: I don't want to send lots of data (e.g. images, audio, ...) to the client, to discover there that the data is useless.
  2. Showing console errors in the browser isn't very helpful. When you read daily the questions on this forum, you will see that lots of users don't even think about having a look at the browser log. I could send the errors back to the server, but that seems a very inefficient flow ...
  3. In some cases it could be used e.g. to change the node status, so other nodes could watch the errors.
  4. ...

@dceejay: Ok, thanks. I understand ...
What a pitty. Could I perhaps workaround this gap like this:

beforeEmit: function(msg, value) {
    if(!Array.isArray(msg.payload)) {
        node.error("The msg.payload should be an array");

        // Since we cannot ignore invalid input messages for UI nodes, 
        // we workaround it by not sending invalid data to the client dashboard...
        msg.payload = null;
    }

    return { msg: msg };
},

Does that makes sense, or have I forgotten something?

Try it. Or maybe return the previous value ( as default is not to send if no change)

Hi guys,

Thanks! The workaround seems to be enough for me:

@dceejay: Do I understand correctly that all message data is compared to the previous message? If yes, isn't that inefficient when streaming e.g. audio or video buffers at high rates.

1 Like