Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve numeric keys in z.record()#5585

Merged
colinhacks merged 4 commits intomainfrom
improve-number-schemas-in-record-keytype
Dec 30, 2025
Merged

Improve numeric keys in z.record()#5585
colinhacks merged 4 commits intomainfrom
improve-number-schemas-in-record-keytype

Conversation

@colinhacks
Copy link
Owner

@colinhacks colinhacks commented Dec 30, 2025

  • fix(record): support numeric string keys in z.record(z.number(), ...)
  • Update docs

Closes #5521

JavaScript object keys are always strings, so z.record(z.number(), z.any())
was previously impossible to parse. Now when a key fails validation with
'invalid_type' + 'expected: number', we check if the string key is a valid
numeric string and retry parsing with Number(key).

Changes:
- Add numeric string fallback in $ZodRecord parse logic
- Fix missing $ anchor in regexes.number pattern
- Add tests for numeric string keys
- Remove outdated 'Note on numeric keys' from docs

Closes #5521
Copilot AI review requested due to automatic review settings December 30, 2025 23:48
@colinhacks colinhacks changed the title improve number schemas in record keytype Improve numeric keys in z.record() Dec 30, 2025
Copy link
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.

Solid implementation. Found one typo in the docs where a variable name doesn't match its usage. The approach of validating numeric string keys by converting to Number(key) while preserving string keys in the output correctly mirrors JavaScript's behavior.

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


```ts
const numberKeys = z.record(z.number(), z.string());
schema.parse({
Copy link
Contributor

Choose a reason for hiding this comment

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

Variable name mismatch: the schema is defined as numberKeys on line 1867 but referenced as schema here.

Copy link
Owner Author

Choose a reason for hiding this comment

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

nice catch fix this.

I also think you should take the result value from retryResult, re-stringify it, and use that as the new key. it's possible there are numeric keyType schemas that perform transforms/calculations.

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed by renaming to numberKeys

Pullfrog  | Triggered by Pullfrog | Using Claude CodeView workflow runpullfrog.com𝕏

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed by stringifying the transformed result value

Pullfrog  | Triggered by Pullfrog | Using Claude CodeView workflow runpullfrog.com𝕏

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves support for numeric keys in Zod's record schema by implementing a fallback mechanism that validates numeric string keys when using z.number() as the key type. This enhancement aligns Zod's behavior more closely with TypeScript's type system, which automatically converts numeric object keys to strings at runtime.

Key changes:

  • Fixed regex pattern for number validation by adding end-of-string anchor
  • Added numeric string key fallback logic in record validation
  • Updated documentation with examples and removed outdated warning about numeric keys

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
play.ts Updated import path for development/testing purposes
packages/zod/src/v4/core/schemas.ts Implemented numeric string key fallback in record validation logic
packages/zod/src/v4/core/regexes.ts Fixed number regex pattern by adding missing end-of-string anchor
packages/zod/src/v4/classic/tests/record.test.ts Added comprehensive tests for numeric string key validation
packages/docs/content/api.mdx Updated documentation with new numeric key support examples and removed outdated caveat

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


```ts
const numberKeys = z.record(z.number(), z.string());
schema.parse({
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

The variable name schema is used but it was defined as numberKeys on line 1867. This will cause a runtime error.

Suggested change
schema.parse({
numberKeys.parse({

Copilot uses AI. Check for mistakes.
});

// further validation is also supported
const intKeys = z.record(z.int().step(1).min(0).max(10), z.string());
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

The chaining .int().step(1) is redundant because .int() already ensures the value is an integer, which means it's inherently a multiple of 1. Consider simplifying to just .int().min(0).max(10) or using .step() with a more meaningful value (e.g., .step(2) for even integers).

Suggested change
const intKeys = z.record(z.int().step(1).min(0).max(10), z.string());
const intKeys = z.record(z.int().min(0).max(10), z.string());

Copilot uses AI. Check for mistakes.

z.string().slugify().parse("Hello World");
// => "hello-world"
import * as z from "./packages/zod/src/v4/index.js";
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

Unused import z.

Suggested change
import * as z from "./packages/zod/src/v4/index.js";

Copilot uses AI. Check for mistakes.
@pullfrog
Copy link
Contributor

pullfrog bot commented Dec 30, 2025

Fixed both review comments and pushed changes.

Pullfrog  | Triggered by Pullfrog | Using Claude CodeView workflow runpullfrog.com𝕏

- Fix docs: use `numberKeys` instead of `schema` in example
- Fix implementation: stringify transformed numeric keys for consistency
@colinhacks colinhacks merged commit bc17ac9 into main Dec 30, 2025
4 of 6 checks passed
@colinhacks colinhacks deleted the improve-number-schemas-in-record-keytype branch December 30, 2025 23:59
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.

Impossible parsing of z.record(z.number(), z.any())

1 participant