8000 Restore "Required" error message for undefined input in v4 locales · Issue #4787 · colinhacks/zod · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Restore "Required" error message for undefined input in v4 locales #4787
Closed
@tobigumo

Description

@tobigumo

Problem

In Zod v3, the English locale provided a user-friendly "Required" message when input is undefined:

v3 behavior:

case ZodIssueCode.invalid_type:
if (issue.received === ZodParsedType.undefined) {
message = "Required";
} else {
message = `Expected ${issue.expected}, received ${issue.received}`;
}
break;

However, in v4, this special handling was removed

case "invalid_type":
return `Invalid input: expected ${issue.expected}, received ${parsedType(issue.input)}`;

and resulting in verbose error messages like:

"Invalid input: expected string, received undefined"

Related discussion (Q&A): #4490

Solution

Restore the v3 behavior in v4 locales for better UX:

English:

if (issue.input === undefined) return "Invalid input: Required";

Japanese:

if (issue.input === undefined) return "無効な入力: 必須項目です";

Breaking Change Notice

This change will update error messages from:

"expected {type}, received undefined"

to:

"Required"

Question: Is it okay to update existing test cases that expect the old verbose format?


I have a working implementation ready in my fork: https://github.com/tobigumo/zod/tree/feat/v4-restore-required-error-message

Ready to submit PR once this approach is approved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0