Live data from Hacker News

Designing the perfect TypeScript schema validation library

vriad.com

21–30 of 77 posts

Re: Designing the perfect TypeScript schema validation library

#21
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!!

Re: Designing the perfect TypeScript schema validation library

#22

No mention of runtypes which looks pretty similar? https://github.com/pelotom/runtypes

Huh. Totally 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 actualyl does support recursive types! My bad! Great lib.

Re: Designing the perfect TypeScript schema validation library

#23
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.

Re: Designing the perfect TypeScript schema validation library

#24

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?

root_axis gave some good examples. Also types like `int` or `decimal` (which you can also pair with branded static types, but they don't actually guarantee anything other than their name. Really any kind of refinement you can imagine on primitive types.

Re: Designing the perfect TypeScript schema validation library

#25
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!!

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!

Re: Designing the perfect TypeScript schema validation library

#26
post #13

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

Yeah, that's really something that feels like it should be a part of the language, because it would be so useful!

It's explicitly one of the language's non-goals[1].

> Add or rely on run-time type information in programs, or emit different code based on the results of the type system. Instead, encourage programming patterns that do not require run-time metadata.

[1] https://github.com/Microsoft/TypeScript/wiki/TypeScript-Desi...

Re: Designing the perfect TypeScript schema validation library

#27

There is also class-validator, which I've used and I find elegant and working well: https://github.com/typestack/class-validator

Huh, not sure how I missed this.

I don't love the class-based declarations: it's a bit verbose and doesn't allow you to use things like the spread operator to "mix in" fields into objects. There's also some redundancy required for basic types:

  @IsString
  firstName: string;
For my purposes, I also needed support for recursive types, unions, and intersections, which I don't believe are supported.

But their validation built-ins go way beyond Zod (IsEmail, Min, Max, Contains, native Date support, etc). Thanks for sharing.

Re: Designing the perfect TypeScript schema validation library

#28
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!!

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

#29

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.

Re: Designing the perfect TypeScript schema validation library

#30
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!

Haha! Timed that well didn’t we...
Post reply on HN