-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Schemas generated by refine do not have the omit method (or that is what typescript says) #1245
Comments
Is this a bug or expected behavior? If it is the latter, can it we changed? |
Refine/SuperRefine also omits |
Yeah, this is working as expected. The output of For instance: const createdAtIsPast = ({ createdAt }: { createdAt: Date }) => createdAt < new Date();
const baseFooSchema = z.object({ createdAt: z.date(), foo: z.string() })
export const bigFooSchema = baseFooSchema.extend({ bar: z.number() }).refine(createdAtIsPast);
export const fooSchema = baseFooSchema.refine(createdAtIsPast); |
That is basically what I'm doing, and my schema files are tedious to
manage, confusing and bigger than I would want. Not to mention that, every
time I need to do a refinement in some other place, I have to come back and
re-organize the existing schemas to export a new base schema and then do
the refinements in a per case basis.
…On Mon, Aug 1, 2022 at 5:09 PM Scott Trinh ***@***.***> wrote:
Yeah, this is working as expected. The output of refine/superRefine/
transform is actually a ZodEffect rather than the initial type. There are
no plans to change that at the moment. One common way to allow sharing of
refinements or transforms is to save a separate function that has a
permissive type as the input so that it can cover all of the necessary
cases.
For instance:
const createdAtIsPast = ({ createdAt }: { createdAt: Date }) => createdAt < new Date();
const baseFooSchema = z.object({ createdAt: z.date(), foo: z.string() })
export const bigFooSchema = baseFooSchema.extend({ bar: z.number() }).refine(createdAtIsPast);export const fooSchema = baseFooSchema.refine(createdAtIsPast);
—
Reply to this email directly, view it on GitHub
<#1245 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AARKJWJG4BFY6I3PZB54T6DVW7SBHANCNFSM52WAQPLQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
--
---
https://danielorodriguez.com
|
Yeah, I know this seems not optimal, but it keeps everything explicit in a way that arbitrarily adding effects on defined schemas did not. There is a whole long history and context here: #264 but here's a bit of a snippet of the issue we're trying to avoid:
and
Also consider this: z.object({ type: z.literal("make me a string") }).transform(() => "i'm a string"); Obviously the object methods wouldn't make sense here, but if you had a chain of different transform, you suddenly have to keep track of when they become object-like or array-like or promise-like or primitive-like, etc. My personal recommendation is to separate out the input validation logic from any "effects" as a matter of principle when writing schemas. You can still just export the logical schemas most of the time, but in cases where you need to extend and apply effects to those schemas, either write a new schema in that same module, or export the base schema and effects to reassemble however you need. |
Hello, first of all, thanks for Zod, it's fantastic.
According to typescript, the objects that have been refined do not have the omit method.
To reproduce you can try something as simple as this
The text was updated successfully, but these errors were encountered: