Live data from Hacker News

Designing the perfect TypeScript schema validation library

vriad.com

11–20 of 77 posts

Re: Designing the perfect TypeScript schema validation library

#11
post #9

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…

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 :)

Re: Designing the perfect TypeScript schema validation library

#12
post #9

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…

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 = 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

#14

I'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

#15
post #9

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…

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…

Looks pretty similar to how I did it with io-ts! I'm pretty surprised that they don't support it by now.

Re: Designing the perfect TypeScript schema validation library

#16
post #9

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…

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 :)

I look forward to that!

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

#17

I'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.

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

#18

Earlier 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?

Min or max length of an input. Any type of string validation like email or something similar that's validated via regex.

Re: Designing the perfect TypeScript schema validation library

#19

I'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.

I should point out that there's nothing in Zod (yet) that isn't expressible in Typescript. I'll eventually get around to doing string/numerical validations (`.isEmail()`, etc) that go beyond TS but currently all functionality has an equivalent in TS.

Re: Designing the perfect TypeScript schema validation library

#20

I'd really like something like a compiler step/plugin to generate the validators from the declared typescript types.

You can generate JSON Schema from TS with this library: https://github.com/YousefED/typescript-json-schema

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).

Post reply on HN