Live data from Hacker News

Designing the perfect TypeScript schema validation library

vriad.com

31–40 of 77 posts

Re: Designing the perfect TypeScript schema validation library

#31

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.

I personally prefer to standardize everything as a function. It also leaves some breathing room in case I decide to include parameters as part of a future API augmentation.

Re: Designing the perfect TypeScript schema validation library

#32

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…

Re: runtypes. Not sure how I missed this, it looks like an excellent tool.

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

#33
I 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 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

#34

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…

Second runtypes! It's really Yup but Typescript-first, which is exactly what a lot of people need, per OP's article. The great thing is that there's no need to standardize these libraries - it's trivial, for instance, to build a quick utility wrapper around https://react-hook-form.com/api/#validationResolver that hooks into your runtime-compiletime schema library of choice!

Re: Designing the perfect TypeScript schema validation library

#35
post #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... .

Whoops! Should be `dogsList.parse(...)`. Also fixed the other typo :) Thanks!!

[deleted]

Re: Designing the perfect TypeScript schema validation library

#36
post #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... .

Ah I see I misread your comment.

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

#37
post #25

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

Consider the thread derailed :P

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

#38

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

Fixed! I'd done exactly zero testing on mobile (built the site from scratch yesterday).

Re: Designing the perfect TypeScript schema validation library

#39
post #33

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

Wow I love it. It seems like a lot of us have encountered similar problems with TS. I am planning to shift what I made into something like yours, using right/left instead of throwing errors.

https://www.npmjs.com/package/presi

Re: Designing the perfect TypeScript schema validation library

#40

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

A goal was you can treat it Exactly like an "Interface". I agree it looks totally nonsensical but I think of it as a text replacement.
Post reply on HN