Skip to content

How create conditional Validator? #3382

Answered by thetutlage
cawa-93 asked this question in Help
Discussion options

You must be logged in to vote

Alright, so there are a few things here with sub-schemas or passing a union of schemas to the request.validate method.

The request.validate method cannot work with a union of schemas ( yes, it is a shortcoming ) as it aims to return a single type after validation vs a union of types.

I have been hesitant to add support for union of schemas because this leads to branching post validation and makes the controllers full of if/else statements.

Let's assume that request.validate(v) would have worked and it will return the template property with a union of

template = { src: string } | { timeout: number }

Now at some stage within your code you will have to write the if/else condition to check if…

Replies: 4 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@cawa-93
Comment options

Comment options

You must be logged in to vote
1 reply
@cawa-93
Comment options

Comment options

You must be logged in to vote
1 reply
@cawa-93
Comment options

Answer selected by cawa-93
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
4 participants