# Get property name in custom validator

**URL:** <https://discourse.nodered.org/t/get-property-name-in-custom-validator/4940>\
**Category:** Developing Nodes\
**Created:** [16 November 2018 23:02 UTC](https://discourse.nodered.org/t/get-property-name-in-custom-validator/4940 "2018-11-16T23:02:00Z")\
**Posts on this page:** 3\
**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:** [16 November 2018 23:02 UTC](https://discourse.nodered.org/t/get-property-name-in-custom-validator/4940/1 "2018-11-16T23:02:00Z")

</div>

Hi folks,

When developing a node, you can apply your own [custom validators](https://nodered.org/docs/creating-nodes/properties#property-validation) to validate the node properties.

```auto
RED.nodes.registerType('myType',{
      defaults: {
           myProperty: {value:'', validate:function(v) {
                debugger; 
           }}
      ...

```

But now I have a case where there are a lot of similar properties. So I would like to add the **same** validator function to all those properties:

```auto
RED.nodes.registerType('myType',{
      defaults: {
           myProperty1: {value:'', validate:myValidationFunction(v)},
           myProperty2: {value:'', validate:myValidationFunction(v)},
           myProperty3: {value:'', validate:myValidationFunction(v)},
           myProperty4: {value:'', validate:myValidationFunction(v)},
      ...

```

But then I _**need to have the property name ("myProperty1", "myProperty2" ...) available inside that validation function**_. But I don't think it is available...  
When I go up one step in the call stack it is still available, but I don't think it is passed to the validate function:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2e5bb3133429e880bb419065e6ce3200857b99e4.png)

Does anybody know a way to get the property name?

Thanks !  
Bart

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [16 November 2018 23:32 UTC](https://discourse.nodered.org/t/get-property-name-in-custom-validator/4940/2 "2018-11-16T23:32:10Z")

</div>

You've already dug into the code to see it isn't passed in... not much to add on that.

The other approach is to pass it in yourself ...

```auto
myProperty1: {value:'', validate:function(v) { return myValidationFunction.call(this,"myProperty1",v) }},
myProperty2: {value:'', validate:function(v) { return myValidationFunction.call(this,"myProperty2",v) }},

```

---

<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:** [17 November 2018 06:07 UTC](https://discourse.nodered.org/t/get-property-name-in-custom-validator/4940/3 "2018-11-17T06:07:08Z")

</div>

Hey Nick,

Thanks for the workaround! Will use that.

> [@knolleary](#):
>
> not much to add on that

You never know 😉

I did a small update of the red.js file to pass the '_ **property** _' name as an extra parameter to the validation function:

```auto
if (valid && "validate" in definition[property]) {
   try {
       valid = definition[property].validate.call(node,value, property);
   } catch(err) {
       console.log("Validation error:",node.type,node.id,"property: "+property,"value:",value,err);
   }
}

```

And then indeed I have the property name available in a validation function with _ **two** _ parameters:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4f7960de660c0bd814a48b79f1ac2b2384bf7c04.png)

And my old validation function with _ **one** _ parameter still works fine (i.e. it still gets the property value):  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2d812408a0a6bf5d05d17f578d4b5b0f6fec7b86.png)

_=\> Existing validators wouldn't be broken, when you should consider to add this change to Node-RED._

And when I remove my change again, my new validator just gets called with an undefined property value:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7a30b6cce10c090e11c1c6dbec51eab45c9270ac.png)

_=\> To avoid that the validators (with two parameters) run into problems on older Node-RED versions (which don't pass the property name), perhaps the [documentation](https://nodered.org/docs/creating-nodes/properties#property-validation) could mention that "you should always check whether property is undefined!"._

Or have I forgotten something?  
It would help me a lot if you would consider to pass the property name to validators in the future ...

Thanks!  
Bart
