Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

If you're going from Zod parsers to JSON schema, are you duplicating that in Typescript? Or can you just go ts-to-zod[1] then zod-to-json-schema?

[1]: https://github.com/fabien0102/ts-to-zod



Zod schemas are the source of truth, from which TS types are inferred and JSON schemas are generated.


Interesting.. I'd rather write typescript types than Zod schemas. I haven't used JSON schema, but going TS to Zod was straightforward and really pleasant


Typescript can't understand types as objects, but it can understand objects as types (using typeof). So starting with objects and generating types is more natural.

Also if you start with objects you can express runtime conditions that are not possible with types, for example maximum sizes.

Zod is quite a bit more general than JSON schema because it can express transformations not just validations.


That way does not give you runtime validation of input.

(Or, requires an extra build step to generate that. It's beyond Typescript itself.)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: