You mention creating object types with optional keys is cumbersome in io-ts. How is that solved in zod, exactly? What allows you to map `foo: union([bar, undefined])` to `foo?: bar | undefined` (note the question mark on the left hand side)? There’s nothing in the declaration to give away why this wouldn’t yield `foo: bar | undefined` which is what I believe you’d get out of io-ts. Looks useful - I would have an easi…
Designing the perfect TypeScript schema validation library
11–20 of 77 posts
Re: Designing the perfect TypeScript schema validation library
#12You mention creating object types with optional keys is cumbersome in io-ts. How is that solved in zod, exactly? What allows you to map `foo: union([bar, undefined])` to `foo?: bar | undefined` (note the question mark on the left hand side)? There’s nothing in the declaration to give away why this wouldn’t yield `foo: bar | undefined` which is what I believe you’d get out of io-ts. Looks useful - I would have an easi…
Here's the Zod equivalent:
const C = z.object({
foo: z.string(),
bar: z.number().optional(),
});
type C = t.TypeOf;
/* {
foo: string;
bar?: number | undefined
} */
And here's the code that pulls this off: type OptionalKeys = {
[k in keyof T]: undefined extends T[k]['_type'] ? k : never;
}[keyof T];
type RequiredKeys = Exclude>;
type ObjectType = {
[k in OptionalKeys]?: T[k]['_type'];
} &
{ [k in RequiredKeys]: T[k]['_type'] };
export class ZodObject extends z.ZodType, // { [k in keyof T]: T[k]['_type'] },
ZodObjectDef
>{
// ...
}Re: Designing the perfect TypeScript schema validation library
#13I'd really like something like a compiler step/plugin to generate the validators from the declared typescript types.
Re: Designing the perfect TypeScript schema validation library
#14I'd really like something like a compiler step/plugin to generate the validators from the declared typescript types.
Re: Designing the perfect TypeScript schema validation library
#15You mention creating object types with optional keys is cumbersome in io-ts. How is that solved in zod, exactly? What allows you to map `foo: union([bar, undefined])` to `foo?: bar | undefined` (note the question mark on the left hand side)? There’s nothing in the declaration to give away why this wouldn’t yield `foo: bar | undefined` which is what I believe you’d get out of io-ts. Looks useful - I would have an easi…
Good question! It wasn't easy to get the question mark on the left-hand side, but it is possible. Here's the Zod equivalent: const C = z.object({ foo: z.string(), bar: z.number().optional(), }); type C = t.TypeOf ; /* { foo: string; bar?: number | undefined } */ And here's the code that pulls this off: type OptionalKeys = { [k in keyof T]: undefined extends T[k]['_type'] ? k : never; }[keyof T]; type RequiredKeys = E…
Re: Designing the perfect TypeScript schema validation library
#16You mention creating object types with optional keys is cumbersome in io-ts. How is that solved in zod, exactly? What allows you to map `foo: union([bar, undefined])` to `foo?: bar | undefined` (note the question mark on the left hand side)? There’s nothing in the declaration to give away why this wouldn’t yield `foo: bar | undefined` which is what I believe you’d get out of io-ts. Looks useful - I would have an easi…
With some effort, you can actually mix optionals and required with io-ts as well. If I ever get permission to open source my io-ts wrapper I'd be glad to show you how :)
It's definitely possible to wrap `io-ts` to get a better interface, especially if you spend some (or rather, a LOT) of time figuring out the type declarations...
Re: Designing the perfect TypeScript schema validation library
#17I'd really like something like a compiler step/plugin to generate the validators from the declared typescript types.
This is basically that but in the opposite direction. It also has the advantage of being able to express validations that you can't express in the type system.
Re: Designing the perfect TypeScript schema validation library
#18Earlier quoted context omitted.
This is basically that but in the opposite direction. It also has the advantage of being able to express validations that you can't express in the type system.
Do you have an example of the kind of validation couldn't be expressed in the type system?
Re: Designing the perfect TypeScript schema validation library
#19I'd really like something like a compiler step/plugin to generate the validators from the declared typescript types.
This is basically that but in the opposite direction. It also has the advantage of being able to express validations that you can't express in the type system.
Re: Designing the perfect TypeScript schema validation library
#20I'd really like something like a compiler step/plugin to generate the validators from the declared typescript types.
It's certainly conceivable to build an equivalent for Zod but you'll eventually want to validate types that aren't expressible in TS (i.e. Integer).