typedInput and custom type validation

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:

      $('#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]+)?$/
          }
        ],
      });

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.

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 ?!

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.

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

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

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

Or generically you can

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

Indeed so simple !
Thanks a lot for your support.

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.