How to use `zod` in a clean architecture setup?
See original GitHub issueHi @colinhacks - great lib!
I would love to use this in our project but right now I am not clear on best to structure the code so that the entities do not end up directly depending on zod
.
Ideally the core entities should not depend on anything, here since they are defined using a zod object that seems impossible?
Thanks again!
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Schema Validation with Zod and Express.js
With zod I can define defaults, so those values can be required in the handler. And that lets me keep default settings a...
Read more >How to use external validation libraries for entities in a "clean ...
import { z } from 'zod'; const Audience = z.object({ name: z.string(), id: z.string(), }); type Audience ...
Read more >Zod Makes TypeScript Even Better - YouTube
Zod is amazing. It is not only an incredible validation library, but the direct interactions it has with TypeScript make it event better....
Read more >Protect your Next.js API with Zod - By Giancarlo Buomprisco
You can play with Zod using the CodeSandbox snippet below. Please drag the sidebar on the left-hand side to see the code, and...
Read more >zod/package.json at master · colinhacks/zod - GitHub
Go to file T · Go to line L · Copy path · Copy permalink.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I guess everything else, how to make the layers should be derivable, or else make specific questions. I’d recommend, if you go FP direction to actually use Item.safeParse(v) and return an actual
Either<L, R>
orResult<E, A>
z.Schema
is abstract interface, does not have API for the specific types.The solution to your problem depends on context of usage. For example you don’t need to use
z.Schema
, as long as usage of ZEnum is somehow constraint to the type.For example:
This will fail type check if ZEnum is not compatible with IUnion. You will see the problem on use, down the line, not directly on ZEnum in this case.
Its in this case not exhaustive, but the moment you remove a value from IUnion, wherever you referred to it will be invalid and fail type-check, whenever you add something, ZEnum.parse will fail, if it misses the value.
You can also create exhaustive APIs like usualy using IUnion too.