3 ms·
I like a lot of things about zod (it's what I use when I need to do this kind of validation), but when I was working with a mildly complicated schema the type t
by ncallaway 3y ago
I like a lot of things about zod (it's what I use when I need to do this kind of validation), but when I was working with a mildly complicated schema the type that z.infer<Schema> produced wasn't great for me.
When I produce a type definition for a nested schema, I'll produce types for individual pieces.
So
type Schema = {
foo: Foo;
bar: Bar;
baz: Baz[];
}
type Foo = {
red: Color;
white: Color;
blue: Color;
}
type Color = {
r: number;
g: number;
b: number;
}
type Bar = {...}
type Baz = {...}
I couldn't find a clean way to decompose the type that z.infer<Schema> produced. It would give me a single type object that was deeply structured. For a pretty flat-simple small schema, z.infer<> was totally fine.
For the more complicated schema it wasn't _terrible_, I still used it. I made it work, but it definitely wasn't the dev experience I was hoping for.
I think something that went the other way would be much preferable for me. I'd rather define the typescript type, then have some validate<Schema>() function that was available to me.
Basically, I think it's _easier_ to get a generated validate() function to play nicely with the rest of my code, than it is to get the inferred type to play nicely with the rest of my code.
- solumunus 3y agoUse objectSchema.shape.xxx to access the nested schemas and infer on those.