-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Create exact error message for z.array().length(). #1611
Comments
Hi @john-schmitz ! I'll provide my opinion based on what I see implemented. Here's the def for length(len: number, message?: errorUtil.ErrMessage): this {
return this.min(len, message).max(len, message) as any;
} That's why you see the behavior described in your issue - However, there's an opportunity to build a more contextually-correct error message precisely in that line that gives a more contextually-correct error message, if you'd like to submit a PR. |
Oh a final note - you don't need a custom refine validation! It's as easy as this const testSchema = z
.array(z.number())
.length(4, { message: "needs to be exactly 4" });
testSchema.parse([]); In case that saves you any trouble. |
fixed with pr #1620 |
Given this schema:
const testSchema = z.array(z.number()).length(4);
For arrays with less than 4 elements, it returns the message:
For arrays with more than 4 elements, it returns the message:
I wish instead for it to return a message explaining that it should be precisely 4 items:
I know I could achieve that with a custom refine validation, but it would be nice to have it baked in.
Of course, if you find this suggestion valid, I could make a PR for it.
The text was updated successfully, but these errors were encountered: