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

"validation" field should be optional on ZodInvalidStringIssue type #3373

Open
Dan503 opened this issue Apr 3, 2024 · 1 comment
Open

"validation" field should be optional on ZodInvalidStringIssue type #3373

Dan503 opened this issue Apr 3, 2024 · 1 comment

Comments

@Dan503
Copy link

Dan503 commented Apr 3, 2024

This is mainly a concern when using superRefine

z.object({ one: z.string(), two: z.string() })
.superRefine((value, context) => {
    if (!value.one && !value.two) {
        context.addIssue({
            code: ZodIssueCode.invalid_string,
            message: 'Need to enter one or two',
            // I do not care what the format of the string is. I only care that a value exists.
            validation: { includes:'' }
        })
    }
});

The above example demonstrates why validation should be optional.

(Note this is a reduced test case, this one could be converted to a simple .refine() but my real world version cannot)

@colinhacks
Copy link
Owner

colinhacks commented Apr 4, 2024

Hm. I think that's a good idea generally but not in your particular case. Since you're trying to enforce an "OR" condition, you should just use ZodIssueCode.invalid_type. By contrast invalid_string is used when a string fails a string-specific validation (too long, too short, failed regex check, etc).

This is all quite nitpicky of course, and I think this is a valid feature request for other use cases where custom string validations are being performed.

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

2 participants