Skip to content

fix(v4): emit falsy prefault values in toJSONSchema (#5824)#5893

Merged
colinhacks merged 1 commit into
colinhacks:mainfrom
mixelburg:fix/falsy-prefault-to-json-schema
Apr 29, 2026
Merged

fix(v4): emit falsy prefault values in toJSONSchema (#5824)#5893
colinhacks merged 1 commit into
colinhacks:mainfrom
mixelburg:fix/falsy-prefault-to-json-schema

Conversation

@mixelburg
Copy link
Copy Markdown
Contributor

What

Fixes #5824

The toJsonSchema function used a truthiness check (result.schema._prefault) when deciding whether to emit a prefault value as a JSON Schema default. This silently dropped falsy prefault values like false, 0, and "".

How

Changed the check from:

if (ctx.io === "input" && result.schema._prefault)

to:

if (ctx.io === "input" && "_prefault" in result.schema)

This ensures all prefault values — including falsy ones — are correctly emitted as JSON Schema defaults.

Reproduction

// Before: default is omitted (wrong)
z.boolean().prefault(false).toJSONSchema({ io: "input" });
// { type: "boolean" }

// After: default is included (correct)
z.boolean().prefault(false).toJSONSchema({ io: "input" });
// { type: "boolean", default: false }

Tests

Added a test case for falsy prefaults (false, 0, ""). All 3713 existing tests pass.

Fixes colinhacks#5824

The  function used a truthiness check ()
which filtered out falsy prefault values like , , and .

Changed to use  so all prefault values are
correctly emitted as JSON Schema defaults.
@pullfrog
Copy link
Copy Markdown
Contributor

pullfrog Bot commented Apr 28, 2026

Reviewed PR #5893 — approved with no issues. The fix correctly replaces a truthiness check (result.schema._prefault) with a property existence check ("_prefault" in result.schema), ensuring falsy prefault values (false, 0, "") are emitted as JSON Schema defaults. Tests pass.

Task list (4/4 completed)
  • Read the PR diff and understand the changes
  • Read the modified source file for full context
  • Verify correctness of the fix and test coverage
  • Submit review

Pullfrog  | View workflow run | via Pullfrog | Using Claude Opus𝕏

@pullfrog
Copy link
Copy Markdown
Contributor

pullfrog Bot commented Apr 28, 2026

TL;DR — Fixes toJSONSchema silently dropping falsy prefault values (false, 0, "") by replacing a truthiness check with a property-existence check. Closes #5824.

Key changes

  • Fix falsy _prefault detection in toJSONSchema — Replace result.schema._prefault (truthiness) with "_prefault" in result.schema (existence) so that false, 0, and "" are emitted as JSON Schema default values.
  • Add tests for falsy prefault values — Cover z.boolean().prefault(false), z.number().prefault(0), and z.string().prefault("") with inline snapshots.

Summary | 2 files | 1 commit | base: mainfix/falsy-prefault-to-json-schema


Before: toJSONSchema checked result.schema._prefault with a truthiness test, so falsy values like false, 0, and "" were silently skipped — the generated JSON Schema had no default field.
After: The check uses "_prefault" in result.schema, so any assigned prefault value — including falsy ones — is correctly emitted as the JSON Schema default.

The root cause is a single-character class of bug: using a value as a boolean guard when the value's domain includes falsy members. The fix is minimal — one conditional changed from truthiness to in — and the test covers all three common falsy types.

to-json-schema.ts · to-json-schema.test.ts

Pullfrog  | View workflow run | via Pullfrog | Using Claude Opus𝕏

Copy link
Copy Markdown
Contributor

@pullfrog pullfrog Bot left a comment

Choose a reason for hiding this comment

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

Reviewed — no issues found.

Task list (4/4 completed)
  • Read the PR diff and understand the changes
  • Read the modified source file for full context
  • Verify correctness of the fix and test coverage
  • Submit review

Pullfrog  | View workflow run | Using Claude Opus𝕏

@colinhacks colinhacks merged commit ebc8287 into colinhacks:main Apr 29, 2026
7 checks passed
@colinhacks
Copy link
Copy Markdown
Owner

Landed in Zod 4.4

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.

Falsy prefault() values are not emitted by toJsonSchema()

2 participants