Skip to content
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

Not Possible to use Zod omit on schema passed to resolver #650

Open
blacksandsolutions opened this issue Dec 4, 2023 · 1 comment
Open

Comments

@blacksandsolutions
Copy link

blacksandsolutions commented Dec 4, 2023

Describe the bug

When omit is used to modify a schema, that schema causes an error when passed to zod resolver.

To Reproduce
I have a base schema for an Account.

export const AccountSchema = z.object({
    uid: z.string().uuid(),
    name: z
        .string({
            invalid_type_error: 'Please enter an account name.',
        })
        .pipe(nonempty),
    type: z.enum(['manual', 'auto'], {
        invalid_type_error: 'Please select a valid account type.',
    }),
})

For create form I extend this schema, omitting the uid

export const CreateAccountSchema = AccountSchema.omit({
    uid: true,
})

When the latter schema is passed to the resolver

Screenshot 2023-12-04 at 8 14 14 PM

I see this error when I try to submit / trigger form etc

Screenshot 2023-12-05 at 8 10 50 PM

If instead I pass AccountSchema like this, there is no error
Screenshot 2023-12-04 at 8 16 28 PM

I'm not able to create code sandbox using this template - it opens in a beta version and there are no files / edit controls..?

Expected behavior
Expect the Schema with omit applied to work the same as the original schema - e.g. not throw an error

  • OS: Mac
  • Browser: Chrome
@blacksandsolutions
Copy link
Author

I've worked around this by spitting my schema up into two parts.

export const BaseAccountSchema = z.object({
    uid: z.string().uuid(),
})

export const CreateAccountSchema = x.object({
    name: z
        .string({
            invalid_type_error: 'Please enter an account name.',
        })
        .pipe(nonempty),
    type: z.enum(['manual', 'auto'], {
        invalid_type_error: 'Please select a valid account type.',
    }),
})

export const AccountSchema = BaseAccountSchema.merge(CreateAccountSchema)

This way I can use the CreateAccountSchema in the form and AccountSchema wherever I need to validate the full object

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

1 participant