3 ms·
As someone who never jumped onto the TypeScript hype-wagon, what is this for? Is this something like clojure.spec by for TypeScript so you can do runtime valida
by diggan 2y ago
As someone who never jumped onto the TypeScript hype-wagon, what is this for? Is this something like clojure.spec by for TypeScript so you can do runtime validation of data instead of compile-time validation?
Basically joi (https://joi.dev/api/?v=17.13.3 https://joi.dev/api/?v=17.13.3) but different in some way?
- VoidWhisperer 2y agoCorrect on the part of it being a runtime validation of data library (I can't as easily speak to whether or not it is similar to joi, never used it)
- _fat_santa 2y agoI think the killer feature of Zod is type inference. Not sure if Joi has support for it yet but you can take a zod schema and wrap it in `z.infer` to get the typescript type. Personally I use zod in my API for body validations, it's super nice to write the schema then just use `type Body = z.infer(schema)` to get the TS type to use inside the code.
- threatofrain 2y agoThat's table stakes for this niche.
- naberhausj 2y agoI've not used either, but it appears to be similar to JOI, yes. The main distinction is that ZOD allows you to extract a TypeScript type from your schema. This means you get both compile-time and run-time type checking.
- valbaca 2y agoyep. Typescript is build-time validation, but in the end TS is "just JS" Zod provides runtime validation (and also works well with TS)
- molszanski 2y agoDeepkit.io has cool validations in runtime based on TS, but it is whole another level.
- newaccountman2 2y ago> Is this something like clojure.spec by for TypeScript so you can do runtime validation of data instead of compile-time not really "instead", more like "in addition to". Even if your code compiles, if you are receiving data, e.g., via API, then you need to check that it actually conforms to the type/schema you expect. What is run is JS, so it, sadly, won't just crash/error if an object that is supposed to be of `type Cat = {name: string, ownerId: number}` lacks an `ownerId` at runtime. Have you used Pydantic in Python? It's like that, but feels worse, IMO lol. I say this because Pydantic fits into writing Python code much more naturally than writing Zod stuff fits into writing TypeScript, IMO.