Scott Trinh
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...
const Pet = z.discriminatedUnion("type", [Cat, Dog]).or(UnknownPet);
const Pet = z.discriminatedUnion("type", [Cat, Dog]).or(UnknownPet);
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...
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...
declare const data: unknown;

const knownData = KnownData.safeParse(data);
if (knownData.success) {
// knownData is a nice discriminated union. Sweet! Do your stuff
return;
}

const unknownData = UnknownData.safeParse(data);
if (unknownData.success) {
// unknownData is parseable as _something_ so do something with that here
return;
}

throw unknownData.error;
declare const data: unknown;

const knownData = KnownData.safeParse(data);
if (knownData.success) {
// knownData is a nice discriminated union. Sweet! Do your stuff
return;
}

const unknownData = UnknownData.safeParse(data);
if (unknownData.success) {
// unknownData is parseable as _something_ so do something with that here
return;
}

throw unknownData.error;
31 replies