Z
Zod•17mo ago
Steve

Conditional validation

Regular union failed unfortunately
11 Replies
Scott Trinh
Scott Trinh•17mo ago
Can we maybe discuss over a concrete implementation? You can use this fork of the Zod sandbox on Codesandbox: https://codesandbox.io/p/sandbox/prod-https-pjskkj?file=/src/index.ts:1,1
prod-https-pjskkj
CodeSandbox is an online editor tailored for web applications.
Scott Trinh
Scott Trinh•17mo ago
I'd like to see some real input data and the desired output data. I think we can maybe get to a schema that works at runtime, even if the type system is unable to encode the logic itself. Basically superRefine and transform can do anything so there isn't anything at runtime we can't achieve, but the types are limited to what can be easily expressed in objects, unions, and intersections. although even the type thing can be overcome with some assertions.
Steve
SteveOP•17mo ago
I will take a look later, currently in meeting oh and I noticed also z.discriminatedUnion isn't working on schema's that have .transform being called on them. I suspect typing might be too strict.
Basically superRefine and transform can do anything so there isn't anything at runtime we can't achieve, but the types are limited to what can be easily expressed in objects, unions, and intersections.
Yeah I'm fully aware, just not fan of doing it in .transform or .superRefine
Scott Trinh
Scott Trinh•17mo ago
Yep, that's a limitation of how we implemented discriminatedUnion to ensure you don't accidentally pass a non-ZodObject.
Steve
SteveOP•17mo ago
Yeah is there an ability to type it slightly different?
Scott Trinh
Scott Trinh•17mo ago
There are several proposals to improve ZodDiscriminatedUnion, but nothing that has been agreed on. Colin wants a fundamentally more flexible approach. FWIW, from the perspective of the types that are inferred, there is no difference between ZodDiscriminatedUnion and ZodUnion, they both produce values of X | Y | Z, but at runtime, ZodDiscriminatedUnion only parses one schema based on the discriminant. It's strictly a runtime optimization (and produces more helpful errors since we know the intent based on the discriminant)m
Steve
SteveOP•17mo ago
Yeah that's why I want it 🙂 Otherwise my error messages truely look like crap
Scott Trinh
Scott Trinh•17mo ago
Colin proposed a solution called switch that allows arbitrary schema selection based on some callback, but that's impossible to introspect, so we've been a little slow to develop that.
Steve
SteveOP•17mo ago
I would love that actually
Scott Trinh
Scott Trinh•17mo ago
as proposed, it would break all of the existing ecosystem libraries that rely on introspection. Like literally all of this: https://discord.com/channels/893487829802418277/893488038477434881/1129099975893209149
Steve
SteveOP•17mo ago
Ehh Whoops.
Want results from more Discord servers?
Add your server