# typedInput - enforcing only string output from JSONata type

**URL:** <https://discourse.nodered.org/t/typedinput-enforcing-only-string-output-from-jsonata-type/75393>\
**Category:** Developing Nodes\
**Tags:** typedinput\
**Created:** [18 February 2023 15:44 UTC](https://discourse.nodered.org/t/typedinput-enforcing-only-string-output-from-jsonata-type/75393 "2023-02-18T15:44:28Z")\
**Posts on this page:** 8\
**Page:** 1

<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:** [18 February 2023 15:44 UTC](https://discourse.nodered.org/t/typedinput-enforcing-only-string-output-from-jsonata-type/75393/1 "2023-02-18T15:44:28Z")

</div>

Hi Node-RED brains-trust. I need to validate a typedInput that allows JSONata BUT I need the result to be a string not JSON.

I can do this with a validate function:

```auto
const f = value.slice(0, 1)
return !( f === '{' || f === '[')

```

Which does mark the node as invalid HOWEVER, when I open the config panel for the node, it does NOT highlight the field in error.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/48b93e64dbca2be4f7d732131716bd6f7b0c4c48.png)

This is in error for example and so should be highlighted in red.

How can I do this?

I see that there is a `RED.validate.typedInput` function but though I've read the code, I can't make sense of it and there does not appear to be any documentation for it.

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [18 February 2023 21:39 UTC](https://discourse.nodered.org/t/typedinput-enforcing-only-string-output-from-jsonata-type/75393/2 "2023-02-18T21:39:02Z")

</div>

You might try something like` RED.validators.regex(^[^]][^}])`. Pardon my regex if not quite right -- I'm rusty.

---

<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:** [18 February 2023 21:59 UTC](https://discourse.nodered.org/t/typedinput-enforcing-only-string-output-from-jsonata-type/75393/3 "2023-02-18T21:59:45Z")

</div>

Hi Mike,

I'm afraid not. While that does indeed do the validation, it does not highlight typedInput fields only standard inputs. Because typedInput's are actually a compound element on-screen. The actual input you define in the HTML is hidden when you turn it into a typedInput.

At the moment, I'm using a really kludgy workaround with a custom validate function that manually changes the outline:

```auto
    function validateIdSource(value) {
        if (value === '') return true
        const f = value.slice(0, 1)
        if ( f === '{' || f === '[' || !isNaN(Number(value)) ) {
            $('#node-input-elementid + .red-ui-typedInput-container').css('border-color', 'red')
            return false
        }
        $('#node-input-elementid + .red-ui-typedInput-container').css('border-color', 'var(--red-ui-form-input-border-color)')
        return true
    }

```

But it means doing a separate function for every typed input because the validate function is only passed the new field value and not the field name.

```auto
#node-input-elementid + .red-ui-typedInput-container

```

That selects the next element after the input field that has the container class applied.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [18 February 2023 22:03 UTC](https://discourse.nodered.org/t/typedinput-enforcing-only-string-output-from-jsonata-type/75393/4 "2023-02-18T22:03:53Z")

</div>

That is truly ugly. Almost worth whingeing to the developers...

---

<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:** [18 February 2023 22:08 UTC](https://discourse.nodered.org/t/typedinput-enforcing-only-string-output-from-jsonata-type/75393/5 "2023-02-18T22:08:58Z")

</div>

> [@drmibell](#):
>
> whingeing to the developers

Funny! I thought I already had 🤣

---

<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:** [19 February 2023 00:59 UTC](https://discourse.nodered.org/t/typedinput-enforcing-only-string-output-from-jsonata-type/75393/6 "2023-02-19T00:59:53Z")

</div>

So essentially you want to use the built-in JSONata type, but apply additional validation to the value?

I can't immediately think of an elegant way of doing that, but it isn't a bit of the code I've touched recently. It certainly isn't a use case anyone had asked for in the past, so I could easily imagine it isn't easily achieved.

If you have any suggestion for what sort of API you'd like the TypedInput to provide to support that type of use case, that might be a useful next step

---

<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:** [19 February 2023 11:44 UTC](https://discourse.nodered.org/t/typedinput-enforcing-only-string-output-from-jsonata-type/75393/7 "2023-02-19T11:44:06Z")

</div>

Thanks for the response Nick.

I think that it might be enough to expand the standard validation - possibly via the undocumented typedInput validation function to allow for custom validation and to include the error highlighting. I guess probably along the lines of the way I did it.

That function would need a way to know what input it applied to though so that it was generic rather than the specific fns I'm having to create.

If it can't be done via a standard validate function, a new action might be needed for `typedInput` that allowed a function to be defined when you tell the editor to turn the standard input to a typed input.

This would let people apply custom validation to the built-in types. Custom types aren't an issue of course since they can have their own validation fns in the definition.

An explanation of what the `RED.validate.typedInput` does would also be welcome - I'd have submitted a PR if I could have worked it out.

---

<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:** [20 April 2023 11:44 UTC](https://discourse.nodered.org/t/typedinput-enforcing-only-string-output-from-jsonata-type/75393/8 "2023-04-20T11:44:43Z")

</div>

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