Paul
Aarktype
•Created by Paul on 4/17/2025 in #questions
Hint for discriminators in union for better error messages?
Hi, I had an issue similar to https://discord.com/channels/957797212103016458/1362079929986191552.
I wanted to have better error messages in case of more complex discriminated unions, which internally aren't "detected" to be discriminated, thus falling back to an O(n) solution, which, in my case, is fine performance-wise, but not error message-wise.
My concrete use case is a dynamic font where the user can select a value in radio group, causing other fields depending on the chosen radio item to be shown. Multiple radio items can lead to the same fields being shown (thus, having discriminators like
0|1
& 2|3
).
The error message I got in the end was set to the union itself (with "sub error messages" also suggesting changing the discriminator). In the end, I want to "lock into" one union, thus showing the correct error messages on the fields inside the union branch that were wrong.
My "fix" for this was to add a discriminatorHint
path to the meta & adding a postprocessing step in the form resolver, "and-ing" the original schema with a schema with the in discriminator values set, thus pruning out the other branches.
Would you consider adding something like discriminatorHint
(or an alternative workaround which also solves this problem) to the core Arktype? I think this is independent of better optimizations regarding discriminated unions, as there'll always be cases that those optimizations wouldn't be able to correctly catch.2 replies