4 ms·
Zod schemas are the source of truth, from which TS types are inferred and JSON schemas are generated.
by franky47 4y ago
Zod schemas are the source of truth, from which TS types are inferred and JSON schemas are generated.
- pcthrowaway 4y agoInteresting.. 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
- zarzavat 4y agoTypescript 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.
- yencabulator 4y agoThat way does not give you runtime validation of input. (Or, requires an extra build step to generate that. It's beyond Typescript itself.)