# Requiring a msg structure - enforcing the presence of fields and attributes

**URL:** <https://discourse.nodered.org/t/requiring-a-msg-structure-enforcing-the-presence-of-fields-and-attributes/81432>\
**Category:** General\
**Created:** [19 September 2023 09:16 UTC](https://discourse.nodered.org/t/requiring-a-msg-structure-enforcing-the-presence-of-fields-and-attributes/81432 "2023-09-19T09:16:40Z")\
**Posts on this page:** 6\
**Page:** 2

<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:** [24 September 2023 11:28 UTC](https://discourse.nodered.org/t/requiring-a-msg-structure-enforcing-the-presence-of-fields-and-attributes/81432/21 "2023-09-24T11:28:40Z")

</div>

Hi,  
so looking at this... we don't need a "just validate" mode as that already works if you set it to only output a JSON object and pass in a JSON object... so that just leave the requirement to check the whole msg... In which case should we ignore then delete the msg.schema part (and ignore msg.\_msgid) so that it only validates what you expect to be the msg ?

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [28 September 2023 17:16 UTC](https://discourse.nodered.org/t/requiring-a-msg-structure-enforcing-the-presence-of-fields-and-attributes/81432/22 "2023-09-28T17:16:51Z")

</div>

I was thinking of something along these [lines](https://flows.nodered.org/node/@gregoriusrippenstein/node-red-contrib-validation-and-documentation) - a node that validates _any_ of env, flow, global or msg object at the top level, not just a property. Additionally it would be nice if that node could generate documentation of the schema.

The node _owns_ the schema so it doesn't _need_ to be passed in on the `msg` object - as with the json node and it allows validation of the _entire_ object that needs validation. The [holistic json schema validation node](https://flows.nodered.org/node/@gregoriusrippenstein/node-red-contrib-validation-and-documentation) does that plus generates documentation for non-developer humans to understand.

I find this particular useful for flows that provide functionality: nodes have clear documentation but flows have no _structured_ representation for requirements. Using a json schema at the entry point to a flow then allows for clear requirements for what a `msg` object needs in order to utilise the flow.

EDIT: The [README](https://www.npmjs.com/package/@gregoriusrippenstein/node-red-contrib-validation-and-documentation) is shown correctly at [npmjs.com](http://npmjs.com) not at [flows.nodered.org](http://flows.nodered.org) --\> readme has pictures & text, which does not seem to be the case when viewed @ [flows.nodered.org](http://flows.nodered.org) (at least for me).

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [30 September 2023 11:47 UTC](https://discourse.nodered.org/t/requiring-a-msg-structure-enforcing-the-presence-of-fields-and-attributes/81432/23 "2023-09-30T11:47:27Z")

</div>

> [@Colin](#):
>
> > [@gregorius](#):
> >
> > Wait what?!?! 🙂 I can define classes in Node-RED?
> 
> Javascript supports classes so yes, you can define classes in a function node.

Sorry, I missed this thread previously. There are many examples in existing nodes where classes are used.

In uibuilder, extensive use is made of singleton classes so that a single class instance can be shared amongst the nodes and amongst uibuilder node instances.

Worth noting however that JavaScript's handling of classes has, until very recently, always been rather "slack" shall we say. Only now is it getting true private class variables and methods for example. Certainly I'm itching for Node-RED to move to node.js v16 as its base so that many of the newer JavaScript features become available.

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [30 September 2023 21:20 UTC](https://discourse.nodered.org/t/requiring-a-msg-structure-enforcing-the-presence-of-fields-and-attributes/81432/24 "2023-09-30T21:20:30Z")

</div>

> [@TotallyInformation](#):
>
> Worth noting however that JavaScript's handling of classes has, until very recently, always been rather "slack" shall we say.

That's kinda what I meant, if I create a class in JS and then did typeof, I'd get "object" - not really a class in the sense of other languages, quoting the CLI:

```auto
prompt> node
Welcome to Node.js v18.17.0.
Type ".help" for more information.
> class Fubar {
... }
undefined
> typeof Fubar
'function'
> var o = new Fubar()
undefined
> typeof o
'object'
>

```

For this use case not particularly useful.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [30 September 2023 21:30 UTC](https://discourse.nodered.org/t/requiring-a-msg-structure-enforcing-the-presence-of-fields-and-attributes/81432/25 "2023-09-30T21:30:45Z")

</div>

> [@gregorius](#):
>
> That's kinda what I meant, if I create a class in JS and then did typeof, I'd get "object" - not really a class in the sense of other languages, quoting the CLI:

Yes, `typeof` is a blunt instrument. You need to use something like `myobj.constructor.name` I think.

Certainly classes are not as fundamental to JavaScript than in Python. However, they are still very useful and getting ever better now that private variables (node v14) and methods (node v16) are available. Of course, constructors and class extensions are also available.

I wouldn't bother to use JS classes for simple tasks but for complex things, they are pretty useful.

Something else suggested `Object.getPrototypeOf(myobj)?.constructor?.name` was more reliable.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 October 2023 21:31 UTC](https://discourse.nodered.org/t/requiring-a-msg-structure-enforcing-the-presence-of-fields-and-attributes/81432/26 "2023-10-14T21:31:08Z")

</div>

Closed on 14 October 2023 21:31 UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.

[Previous page](https://discourse.nodered.org/t/requiring-a-msg-structure-enforcing-the-presence-of-fields-and-attributes/81432.md?page=1)
