question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use `zod` in a clean architecture setup?

See original GitHub issue

Hi @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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
akommcommented, Dec 7, 2021
  • You can inverse the dependency, so type Item is not derived from const Item, but const Item is typechecked against type Item.
type Item = {
  id: string
}
const Item: z.Schema<Item> = z.object({id: z.string()})
  • You can map schema API simply via a function & interface, hidding zod:
// use interface
type ParseItem = (v: unknown) => Item
const parseItem = (v: unknown) => Item.parse(v)
// not parseItem = Item.parse because of
// https://github.com/colinhacks/zod/issues/808
// and also because parse has more than 1 argument, even though optional

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> or Result<E, A>

1reaction
akommcommented, Dec 16, 2021

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:

const parse = (v: unknown): IUnion => ZEnum.parse(v)

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found