close
Skip to content

fix: add options to invalid discriminator errors - #5723

Merged
colinhacks merged 2 commits into
colinhacks:mainfrom
Danielchinasa:fix/discriminator-error-options
Apr 27, 2026
Merged

colinhacks merged 2 commits into
colinhacks:mainfrom
Danielchinasa:fix/discriminator-error-options

Conversation

@Danielchinasa

@Danielchinasa Danielchinasa commented Feb 18, 2026 •

Copy link
Copy Markdown
Contributor

Title:

fix: add options to invalid discriminator errors

Description:

## Summary

Restores the `options` field to invalid discriminator errors that was present in Zod 3 but missing in Zod 4.

## Changes

- Added `options: Array.from(disc.value.keys())` to invalid discriminator error in `schemas.ts`
- Updated English locale to display valid discriminator values in error message
- Updated snapshot test to reflect new behavior

## Before

```json
{
  "code": "invalid_union",
  "discriminator": "type",
  "message": "Invalid input"
}
```

After

{
  "code": "invalid_union",
  "discriminator": "type",
  "options": ["fruit", "vegetable"],
  "message": "Invalid discriminator value. Expected 'fruit' | 'vegetable'"
}

Testing

  • All existing tests pass
  • Updated snapshot test to reflect improved output
  • Manually tested with example code

Fixes #5670

- Add options field to invalid_union errors for discriminated unions
- Update English locale to display valid discriminator values
- Update snapshot test to reflect new behavior
- Fixes colinhacks#5670

@pullfrog pullfrog Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Low urgency. The runtime behavior is correct and the approach is sound. The main issue is a missing type definition update — options is populated at runtime but not declared on $ZodIssueInvalidUnionNoMatch, so TypeScript users can't access it without a type assertion.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | pullfrog.com | 𝕏

Comment thread packages/zod/src/v4/core/schemas.ts Resolved
Comment thread packages/zod/src/v4/locales/en.ts Resolved
The options field was populated at runtime for discriminated union
errors but not declared on the interface, requiring type assertions
to access it. This adds the missing type definition.
@colinhacks

Copy link
Copy Markdown
Owner

@pullfrog re-review

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No new issues. Reviewed the following changes:

  • Added readonly options?: util.Primitive[] to $ZodIssueInvalidUnionNoMatch so TypeScript users can access discriminator options without a type assertion

Prior review feedback:

  • Declare options on $ZodIssueInvalidUnionNoMatch in errors.ts — addressed in b906dc8

Pullfrog  | View workflow run | Using Claude Opus | 𝕏

@colinhacks
colinhacks merged commit 2e5b23d into colinhacks:main Apr 27, 2026
11 checks passed
@colinhacks

Copy link
Copy Markdown
Owner

Merged. Nice to have options back on the discriminator-mismatch case of discriminatedUnion — restores the v3 behavior and makes the default message actually actionable. Thanks for the follow-up declaring the field on the issue type too, so reading it doesn't require a type assertion 👍

Note: this comment was produced by an AI coding assistant.

@colinhacks

Copy link
Copy Markdown
Owner

Landed in Zod 4.4

This branch was successfully deployed

1 active deployment
Preview – zod-v4 — b906dc8c Deployed Feb 18, 2026 by vercel[bot]
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.

Invalid discriminator errors in Zod 4 do not list possible options like Zod 3

2 participants