# Validate input message in UI node

**URL:** <https://discourse.nodered.org/t/validate-input-message-in-ui-node/11393>\
**Category:** Developing Nodes\
**Created:** [21 May 2019 21:13 UTC](https://discourse.nodered.org/t/validate-input-message-in-ui-node/11393 "2019-05-21T21:13:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [21 May 2019 21:13 UTC](https://discourse.nodered.org/t/validate-input-message-in-ui-node/11393/1 "2019-05-21T21:13:23Z")

</div>

Hi folks,

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

```auto
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

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [21 May 2019 22:30 UTC](https://discourse.nodered.org/t/validate-input-message-in-ui-node/11393/2 "2019-05-21T22:30:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [22 May 2019 18:35 UTC](https://discourse.nodered.org/t/validate-input-message-in-ui-node/11393/3 "2019-05-22T18:35:24Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4ab0826d05218976b2060f877c5835c5a992a2e6.png)

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/45580327973e2140935ee0316d618ff89244b4d1.png)

Anybody have a suggestion?

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [22 May 2019 18:47 UTC](https://discourse.nodered.org/t/validate-input-message-in-ui-node/11393/4 "2019-05-22T18:47:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 May 2019 18:57 UTC](https://discourse.nodered.org/t/validate-input-message-in-ui-node/11393/5 "2019-05-22T18:57:45Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [22 May 2019 19:13 UTC](https://discourse.nodered.org/t/validate-input-message-in-ui-node/11393/6 "2019-05-22T19:13:57Z")

</div>

> [@hotNipi](#):
>
> cover all possible situations

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

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](#):
>
> We can certainly add that capability but obviously the level of dashboard required cannot be assumed by your node. (for a while)

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 May 2019 19:24 UTC](https://discourse.nodered.org/t/validate-input-message-in-ui-node/11393/7 "2019-05-22T19:24:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [22 May 2019 20:56 UTC](https://discourse.nodered.org/t/validate-input-message-in-ui-node/11393/8 "2019-05-22T20:56:35Z")

</div>

Hi guys,

Thanks! The workaround seems to be enough for me:

- The validation information becomes available in the server side log:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/877125a488d7336899838ac400e48053e6e6d5cf.png)
- The '_null_' payload is detected in the client side dashboard, so the empty message can be ignored:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/b/b9e7b994e5ee928f452286a2a6485c2ce4742f63.png)

> [@dceejay](#):
>
> Or maybe return the previous value ( as default is not to send if no change)

@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.
