Extending primitives with custom validation methods (that are chainable & under the same export) #516
Replies: 4 comments 4 replies
-
Did anyone ever come up with an answer to this? I'm struggling with it currently |
Beta Was this translation helpful? Give feedback.
-
I solved this issue in this way // validation.ts
import { z as zod } from 'zod'
export const z = {
...zod,
id: () => zod.number().int().min(0),
} simple and readable, but I have one issue with |
Beta Was this translation helpful? Give feedback.
-
any updates? |
Beta Was this translation helpful? Give feedback.
-
Here is the approach for extending Zod recommended by Colin: zod/plugin/effect/src/index.ts Lines 21 to 31 in 90efe7f |
Beta Was this translation helpful? Give feedback.
-
I'm creating a new discussion for this as I want to avoid spamming the previous discussion and also because I think it will be more discoverable/searchable for others that may have this issue as well.
Original messages from #366
@colinhacks, thank you for showing me the proper way to subclass
ZodString
. Unfortunately, I still can't make this work.This is my Zod abstraction
zod-extended.ts
:This works when I'm doing something like this:
However, it doesn't let me chain my custom method with other
ZodString
methods.BTW in yup this is pretty straightforward using the addMethod helper. I wish Zod had something like this.
Beta Was this translation helpful? Give feedback.
All reactions