Skip to content

fix: ZodFlattenedError accepts a schema, matching inferFlattenedErrors - #6340

Open
deepshekhardas wants to merge 2 commits into
colinhacks:mainfrom
deepshekhardas:fix/pr-5029-flattened-error-schema
Open

fix: ZodFlattenedError accepts a schema, matching inferFlattenedErrors#6340
deepshekhardas wants to merge 2 commits into
colinhacks:mainfrom
deepshekhardas:fix/pr-5029-flattened-error-schema

Conversation

@deepshekhardas

Copy link
Copy Markdown
Contributor

Fixes #5029.

z.ZodFlattenedError was a direct alias of core.$ZodFlattenedError, which takes the schema's output type as its generic. The v3 z.inferFlattenedErrors it's meant to replace takes a schema. So a straight migration — inferFlattenedErrors<typeof schema>ZodFlattenedError<typeof schema> — keyed fieldErrors over the schema's internals (_zod, def, type, ...) instead of the actual fields.

This makes the deprecated alias accept both:

// both now resolve to the same type
type A = z.ZodFlattenedError<typeof schema>["fieldErrors"];
type B = z.ZodFlattenedError<z.output<typeof schema>>["fieldErrors"];
type C = z.inferFlattenedErrors<typeof schema>["fieldErrors"];

When passed a $ZodType, it unwraps to output<T>; otherwise it's treated as a plain output type, so existing callers passing z.output<typeof schema> keep their current typing. Added a type test asserting ZodFlattenedError<typeof schema> is equal to both ZodFlattenedError<output<typeof schema>> and inferFlattenedErrors<typeof schema>.

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

Successfully merging this pull request may close these issues.

ZodFlattenedError does not return the same type as the old inferFlattenedErrors

2 participants