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.
Designing the perfect TypeScript schema validation library
31–40 of 77 posts
Re: Designing the perfect TypeScript schema validation library
#32This 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…
No support for recursive types (which I personally want/need for my project).
I really like their API for constraint checking...might have to steal that...
[UPDATE] runtypes does support recursive types! My bad!
Re: Designing the perfect TypeScript schema validation library
#33I explained it was all about the type inference from the schema so I could make guarantees across runtime boundaries.
This is what I came up with, though this library looks like it may be more comprehensive than what I wrote. Struggled to come up with a good name to represent exactly what it did...
https://www.npmjs.com/package/type-safe-validator
The ability of the TypeScript compiler to allow this stuff continues to make it one of my favourite languages to work with, despite it still being JavaScript underneath.
Re: Designing the perfect TypeScript schema validation library
#34This 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…
Re: Designing the perfect TypeScript schema validation library
#35Possibly 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... .
Whoops! Should be `dogsList.parse(...)`. Also fixed the other typo :) Thanks!!
Re: Designing the perfect TypeScript schema validation library
#36Possibly 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... .
You're right, Cujo also shouldn't have validated. In a previous version of the post `neutered` was nullable and `age` was optional, but I decided to save the discussion of nullables/optional until later so I changed it. Good catch!!
Re: Designing the perfect TypeScript schema validation library
#37Earlier quoted context omitted.
Whoops! Should be `dogsList.parse(...)`. Also fixed the other typo :) Thanks!!
I don’t want to derail the thread, but just a heads up: the layout is very broken on my phone (iPhone). The text is clipped on the left side which basically makes the article unreadable!
Just fixed this! I'd done exactly zero testing on mobile (built the site from scratch yesterday).
Re: Designing the perfect TypeScript schema validation library
#38Earlier quoted context omitted.
Whoops! Should be `dogsList.parse(...)`. Also fixed the other typo :) Thanks!!
Sorry to be a pain but even a few rules (even resetting some things) would make it much easier to read on mobile!
Re: Designing the perfect TypeScript schema validation library
#39I built something like this a few months ago but struggled to explain to almost everyone except die hard TypeScript fans what the big deal was. I explained it was all about the type inference from the schema so I could make guarantees across runtime boundaries. This is what I came up with, though this library looks like it may be more comprehensive than what I wrote. Struggled to come up with a good name to represent…
Re: Designing the perfect TypeScript schema validation library
#40Wow I made the same thing that I put v1 up a couple days ago. https://github.com/tetranoir/presi A main difference is I tried to get rid of having a separate line to create the Type and I tried to get rid of having to learn a new libary's api as much as possible.
This looks interesting too, but the syntax fells really weird to me.