resolvers
resolvers copied to clipboard
Not Possible to use Zod omit on schema passed to resolver
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
I see this error when I try to submit / trigger form etc
If instead I pass AccountSchema
like this, there is no error
I'm not able to create code sandbox using this template - it opens in a beta version and there are no files / edit controls..?
- Zod resolver Template
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
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
I encountered this issue too, and my solution was quite similar to yours.
Here’s an example with the same context:
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.',
}),
})
And then put the attribute to undefined...
export const AccountSchema = BaseAccountSchema.merge(z.object({ uid: z.undefined() }));
This workaround isn't perfect, but it was the best solution for my context. I wish omit worked out of the box.