Skip to content

fix(experimental): allow null with optional and default: null - #201

Merged
ping-maxwell merged 2 commits into
better-auth:experimentalfrom
ping-maxwell:experimental
Sep 1, 2026
Merged

fix(experimental): allow null with optional and default: null#201
ping-maxwell merged 2 commits into
better-auth:experimentalfrom
ping-maxwell:experimental

Conversation

@ping-maxwell

Copy link
Copy Markdown
Collaborator

Summary

  • optional: true accepts null as well as undefined (types + validate)
  • default: null is valid on all schema helpers; absence produces null without type-checking it as the field type

Test plan

  • pnpm test in packages/experimental
  • v.string({ optional: true, default: null }) typechecks and validates omit/nullnull

@pkg-pr-new

pkg-pr-new Bot commented Sep 1, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/better-call@201

commit: 33a60da

@ping-maxwell
ping-maxwell merged commit 65fa992 into better-auth:experimental Sep 1, 2026
5 checks passed
@greptile-apps

greptile-apps Bot commented Sep 1, 2026

Copy link
Copy Markdown

Greptile Summary

The PR updates the experimental schema system so optional fields accept null and all schema helpers support null defaults, aligning related function-schema and capability types.

  • Adds null-aware input and output inference for optional and defaulted schemas.
  • Returns null defaults without validating them as the schema's underlying field type.
  • Expands runtime and compile-time coverage across primitive, object, array, union, and function schemas.

Confidence Score: 4/5

The null-default input contract should be corrected before merging because callers can pass an explicitly permitted null that runtime validation rejects.

Non-optional schemas with default: null now infer null as valid caller input, but the validator only substitutes defaults for undefined or optional null, leaving explicit null to fail the underlying type check.

Files Needing Attention: packages/experimental/src/schema.ts, packages/experimental/src/schema-dx.test.ts

Fix all with Greploop Fix All in Cursor Fix All in Codex Fix All in Claude Code

Reviews (1): Last reviewed commit: "Merge branch 'experimental' into experim..." | Re-trigger Greptile

Comment on lines 368 to +370
? T | null
: T
: null extends D
? T | null

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Null input contract diverges

When a caller explicitly passes null to a non-optional schema with default: null, InferArgs accepts the call but validate does not apply the default and throws a validation error. For example, { n: null } type-checks for v.object({ n: v.number({ default: null }) }) but fails at runtime with expected number, received null.

Knowledge Base Used: Validation and schema contracts

Fix in Cursor Fix in Codex Fix in Claude Code

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.

1 participant