Live data from Hacker News

Designing the perfect TypeScript schema validation library

vriad.com

1–10 of 77 posts

Re: Designing the perfect TypeScript schema validation library

#5
This is great. I recently built a fairly large amount of functionality around io-ts, adding support for mixed required/optional fields, eliminating the need to deal with `Either` monads, and a whole lot of other stuff that's mostly just relevant to my company's usage. I think if this had been available at the time I was looking for an underlying library, it would have been a no brainer to choose this one.

Worth noting, another option is Runtypes[1], which also looks great. I can't remember off the top of my head why I ultimately picked io-ts over Runtypes, but it's another one for folks to consider (and I'd be curious what the Zod author thinks of it).

https://github.com/pelotom/runtypes

Re: Designing the perfect TypeScript schema validation library

#7

This is great. I recently built a fairly large amount of functionality around io-ts, adding support for mixed required/optional fields, eliminating the need to deal with `Either` monads, and a whole lot of other stuff that's mostly just relevant to my company's usage. I think if this had been available at the time I was looking for an underlying library, it would have been a no brainer to choose this one. Worth notin…

Oh, I do want to add one bit of minor feedback for the author: one of the things I like about the io-ts interface is that fields/codecs are values unless they require a parameter (e.g. `t.string` vs `z.string()`). It's a little bit easier for me to read. It's also not clear to me at a glance whether calling `z.string()` is creating a new instance of something and whether that affects the behavior of a given schema.

Re: Designing the perfect TypeScript schema validation library

#8
Possibly stupid question, but why does the dog array validate:

  const dogsList = z.array(dogSchema);

  dogSchema.parse([
   { name: 'Cujo', neutered: null },
   { name: 'Fido', age: 4, neutered: true },
  ]); // passes
Since 'Cujo' doesn't have an age? Assuming it's the same dogSchema as in the previous block, age is required, right?

Oh, and a minor typo: This lets you confidently This way you can confidently....

Re: Designing the perfect TypeScript schema validation library

#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 easier time introducing this than io-ts.

Post reply on HN