typedInput - enforcing only string output from JSONata type

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:

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

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.

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

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:

    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.

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

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

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

Funny! I thought I already had :rofl:

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

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.

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