8000 Support merging ZodEffects (created from an object) into other objects · Issue #1147 · colinhacks/zod · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Support merging ZodEffects (created from an object) into other objects #1147

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

Closed
Aaronius opened this issue May 17, 2022 · 6 comments
Closed

Comments

@Aaronius
Copy link

I've created a ZodEffects schema as follows:

const myObjSchema = z.object({
    foo: z.string(),
    bar: z.string().optional(),
}).refine( input => {
    // allows bar to be optional only when foo is 'foo'
    if ( input.foo !== 'foo' && input.bar === undefined ) return false
    return true
});

Now I want to merge myObjSchema into another object, like so:

const composedSchema = z.object({
  baz: s.string()
}).merge(myObjSchema);

But I can't, because merge is expecting an AnyZodObject argument and not a ZodEffects argument.

@colinhacks
Copy link
Owner

Correct, after a .refine you no longer has a ZodObject instance. Using .and here would work (in which case the final schema would be a ZodIntersection).

@mabitten
Copy link
mabitten commented Apr 5, 2024

Has someone managed to resolve that? I have this problema, I use .refine and now want to use .pick but it not exist on ZodEffect.

@oliverterrell
Copy link

This chaotic end-typing has apparently been reworked in Zod 4 to a single consistent type. I can't wait for the release 😃

@donaldpipowitch
Copy link

This chaotic end-typing has apparently been reworked in Zod 4 to a single consistent type.

Can I read about this somewhere? Would it mean I could use pick on a refined object schema?

@donaldpipowitch
Copy link

A maybe you meant this: #2474 (comment)

@Tuscan-blue
Copy link

Maybe try this: #1147(comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
0