Skip to content

registry.remove() deletes an id that now belongs to another schema, dropping it from toJSONSchema(registry) #6640

Open

Description

@andrewstellman

add() lets a later schema take over an id (#5574, pinned by the "re-registering same id silently overwrites" test), but remove() deletes _idmap[id] for any schema whose metadata carries that id, including one that no longer owns it:

import * as z from "zod";

const r = z.registry<{ id?: string }>();
const a = z.string();
const b = z.number();
r.add(a, { id: "x" });
r.add(b, { id: "x" });
r.remove(a);

r.has(b);          // true
z.toJSONSchema(r); // { schemas: {} }   expected { schemas: { x: { type: "number" } } }

Reproduced on main at 2bf7b06. The fix is a one-line guard in remove() (only delete the id entry when it still points at the schema being removed), with a test, on a branch since PRs are limited to collaborators: main...andrewstellman:zod:registry-remove-owner.

Found by Quality Playbook, an AI code-review tool, with Claude; I reviewed the change.

Activity

  1. anuj-hrcg commented on Oct 1, 2026

    @anuj-hrcg

    Could a maintainer review this independent fix and cherry-pick it if suitable? GitHub denied my account permission to create an upstream PR.

    Compare · Commit

    This implements the ownership guard proposed by andrewstellman: removing the previous ID owner preserves the replacement. The regression fails before and passes afterward, including repeated removal and JSON Schema export. Local remove/add timings were approximately 101/103 ns before/after, retained registry memory was unchanged, and gzip bundle growth was 7 bytes for classic and 8 for Mini.

    All 8,323 tests, type checks, build, lint/format, and resolution/integration checks passed locally with Nub 0.8.3 and pinned Node 24.21.0; hooks were not bypassed. Performance numbers are local estimates, and hosted CI has not run. Prepared with AI assistance and verified locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions