Skip to content

Can an array(union()) schema filter out invalid elements, rather than fail? #493

Answered by fabian-hiller
drooJohnson asked this question in Q&A
Discussion options

You must be logged in to vote

Just a note, I don't believe I can use variant() instead of union() in our actual code, because the literal we're using to differentiate each type is nested a few levels deep, and it doesn't look like there's any way to target a deeply nested discriminator key.

Try targeting the root key that contains the nested key. This should work.

Can an array(union()) schema filter out invalid elements, rather than fail?

Currently this functionality is not supported by default. Since Valibot follows a modular architecture, it can be easily extended with custom code. If you want, I can provide the code for a filterArray function that does exactly what you want. If more users are interested in this…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@drooJohnson
Comment options

@fabian-hiller
Comment options

Answer selected by drooJohnson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants