# typedInput and custom type validation

**URL:** <https://discourse.nodered.org/t/typedinput-and-custom-type-validation/13098>\
**Category:** Developing Nodes\
**Created:** [10 July 2019 08:34 UTC](https://discourse.nodered.org/t/typedinput-and-custom-type-validation/13098 "2019-07-10T08:34:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thierry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thierry/32/8654_2.png) [@Thierry](https://discourse.nodered.org/u/Thierry)\
**Post date:** [10 July 2019 08:34 UTC](https://discourse.nodered.org/t/typedinput-and-custom-type-validation/13098/1 "2019-07-10T08:34:00Z")

</div>

Hi,

I have a typedInput field which uses a custom type. My custom type has a specific validate function which is working well inside the property panel (when the entered value does not match the validate pattern, then the input field has a red border), but this validate function does not prevent the node from being deployed.

Actually I was expecting the custom type validate function to behave like for the predefined types (flow, global, json, num, ...) meaning when the entered value in typedInput field does not match the required type, then the node appears with the red triangle on the workspace and a popup warning message is displayed at deploy, but it seems not the case for the custom type.

Is it a normal behavior ? Is there a way to workaround ?

For information here is my typedInput field declaration. The issue is related to custom type where the validate function does not prevent to deploy when entering wrong values (for instance "blablabla") in field:

```auto
      $('#node-input-frequency').typedInput({
        default: '0',
        typeField: "#node-input-propertyType",
        types: [
          {
            value: "1",
            label: "1Hz",
            hasValue: false
          },
          {
            value: "10",
            label: "10Hz",
            hasValue: false
          },
          {
            value: "100",
            label: "100Hz",
            hasValue: false
          }, {
            value: "1000",
            label: "1000Hz",
            hasValue: false
          },
          {
            value: "custom",
            label: "Custom (in Hz) :",
            validate: /^[0-9]*(\.[0-9]+)?$/
          }
        ],
      });

```

---

<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:** [10 July 2019 08:58 UTC](https://discourse.nodered.org/t/typedinput-and-custom-type-validation/13098/2 "2019-07-10T08:58:52Z")

</div>

There are two different types of validation.

- validation of a node's properties - specific in the `defaults` property of your node definition.
- validation of values entered into a TypedInput component - defined in the custom type you provide the TypedInput.

There is nothing to tell Node-RED to use the TypedInput's validation when validating individual node properties.

You'd have to add your own validation function to the `frequency` property in your node's defaults list.

---

<div class="post-metadata">

**Author:** ![Thierry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thierry/32/8654_2.png) [@Thierry](https://discourse.nodered.org/u/Thierry)\
**Post date:** [10 July 2019 09:26 UTC](https://discourse.nodered.org/t/typedinput-and-custom-type-validation/13098/3 "2019-07-10T09:26:52Z")

</div>

> [@knolleary](#):
>
> in your node's defaults list.

I already have this in default list for frequency property :  
`frequency: { validate: RED.validators.typedInput("propertyType") },`

which I guess is validating the frequency against propertyType ?!

---

<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:** [10 July 2019 09:27 UTC](https://discourse.nodered.org/t/typedinput-and-custom-type-validation/13098/4 "2019-07-10T09:27:55Z")

</div>

That will only work for the built-in types. At the point this is called, it doesn't know about your custom type and validator.

---

<div class="post-metadata">

**Author:** ![Thierry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thierry/32/8654_2.png) [@Thierry](https://discourse.nodered.org/u/Thierry)\
**Post date:** [10 July 2019 09:31 UTC](https://discourse.nodered.org/t/typedinput-and-custom-type-validation/13098/5 "2019-07-10T09:31:37Z")

</div>

OK then what should I do ?  
Is there already some existing examples I can refer to ?

---

<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:** [10 July 2019 09:33 UTC](https://discourse.nodered.org/t/typedinput-and-custom-type-validation/13098/6 "2019-07-10T09:33:56Z")

</div>

It looks like you already have a regex to validate the field, so you can reuse that:

```auto
frequency: { validate: RED.validators.regex(/^[0-9]*(\.[0-9]+)?$/) }

```

---

<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:** [10 July 2019 09:36 UTC](https://discourse.nodered.org/t/typedinput-and-custom-type-validation/13098/7 "2019-07-10T09:36:18Z")

</div>

Or generically you can

```
frequency: { validate: function(v) { do some test on v that returns true or false } }
```

---

<div class="post-metadata">

**Author:** ![Thierry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thierry/32/8654_2.png) [@Thierry](https://discourse.nodered.org/u/Thierry)\
**Post date:** [10 July 2019 09:39 UTC](https://discourse.nodered.org/t/typedinput-and-custom-type-validation/13098/8 "2019-07-10T09:39:49Z")

</div>

> [@knolleary](#):
>
> RED.validators.regex(/\[1\]\*(.[0-9]+)?$/)

Indeed so simple !  
Thanks a lot for your support.

* * *

1. 0-9

---

<div class="post-metadata">

**Author:** ![Thierry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thierry/32/8654_2.png) [@Thierry](https://discourse.nodered.org/u/Thierry)\
**Post date:** [10 July 2019 09:42 UTC](https://discourse.nodered.org/t/typedinput-and-custom-type-validation/13098/9 "2019-07-10T09:42:09Z")

</div>

Ok this I know but I was a bit confused between validation of a node's properties and validation of values entered into a TypedInput component.  
Now it's fine.  
Thanks for your support.
