We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Currenty, the only way to create a shortcut is to create a function and wrap the schema inside it.
function emptyOrOptional(schema: ZodString): ZodString { return z.union([schema.optional(), z.string().max(0)]); } const validationSchema = emptyOrOptional(z.string().email())
This results in 2 different sytanxes, chaining and wrapping.
Would it be possible to create something similar to yup's addMethod, which would result in following code?
z.addMethod('emptyOrOptional',<params>); const validationSchema = z.string().email().emptyOrOptional()
The text was updated successfully, but these errors were encountered:
This is achievable using typescript and some prototype pollution
// zod-custom.d.ts export * from 'zod'; declare module 'zod' { interface ZodString { allowEmpty(): ZodString; } } // zod-custom.ts import { z, ZodString } from 'zod'; z.ZodString.prototype['allowEmpty'] = function () { return this.or(z.string().max(0)) as unknown as ZodString; };
Maybe documentation would be enough in this case
Sorry, something went wrong.
No branches or pull requests
Currenty, the only way to create a shortcut is to create a function and wrap the schema inside it.
This results in 2 different sytanxes, chaining and wrapping.
Would it be possible to create something similar to yup's addMethod, which would result in following code?
The text was updated successfully, but these errors were encountered: