Scott Trinh
ZZod
•Created by Tango on 5/7/2025 in #questions
Tango - I have the need to handle discriminated...
I guess you could tweak my original "two phase parse" suggestion and examine the error from the strict parsing to see if it was
{ type: "Cat" }
-like.31 replies
ZZod
•Created by Tango on 5/7/2025 in #questions
Tango - I have the need to handle discriminated...
"These are values that are a superset of a known shape, but a completely opaque
unknown
properties". And whatever you can reasonably do with that seems fine. Including going back and checking if they had a type from the known union and complaining.31 replies
ZZod
•Created by Tango on 5/7/2025 in #questions
Tango - I have the need to handle discriminated...
Basically, you want
{ type: "Cat" }
to fail, but there is no way to reasonably do that statically without a bunch of dynamic code. In my personal opinion, I would embrace that and treat it the same as { type: "Spaceship", origin: "Alderaan" }
31 replies
ZZod
•Created by Tango on 5/7/2025 in #questions
Tango - I have the need to handle discriminated...
I'm not sure I follow this question/comment. What's the specific issue with this kind of blackboxed approach?
31 replies
ZZod
•Created by Tango on 5/7/2025 in #questions
Tango - I have the need to handle discriminated...
That seems correct to me since it's not discriminated.
31 replies
ZZod
•Created by Tango on 5/7/2025 in #questions
Tango - I have the need to handle discriminated...
Works fine too, and gives you a slight boost if your union contains many members
31 replies
ZZod
•Created by Tango on 5/7/2025 in #questions
Tango - I have the need to handle discriminated...
yeah, you cannot use the
discriminatedUnion
schema type here, you have to use union
. Maybe you can mix it with .or
31 replies
ZZod
•Created by Tango on 5/7/2025 in #questions
Tango - I have the need to handle discriminated...
31 replies
ZZod
•Created by Tango on 5/7/2025 in #questions
Tango - I have the need to handle discriminated...
Basically make a transform that maps it into your final discriminated type and just black boxes the data.
31 replies
ZZod
•Created by Tango on 5/7/2025 in #questions
Tango - I have the need to handle discriminated...
Another more principled approach is to make a known sentinel schema to "catch" these cases and transform them into the discrimination. That makes it at least somewhat easier to pass downstream.
31 replies
ZZod
•Created by Tango on 5/7/2025 in #questions
Tango - I have the need to handle discriminated...
but at the type level you still get a non-discriminated union.
31 replies
ZZod
•Created by Tango on 5/7/2025 in #questions
Tango - I have the need to handle discriminated...
well, I guess it could be a little better than
union
at the moment since it short-circuits for the known types.31 replies
ZZod
•Created by Tango on 5/7/2025 in #questions
Tango - I have the need to handle discriminated...
FWIW, I think your "tolerant" schema here is no better than
union
31 replies
ZZod
•Created by Tango on 5/7/2025 in #questions
Tango - I have the need to handle discriminated...
31 replies