Skip to content

feat(zod): add Zod v4 schema serializer#82

Draft
mafredri wants to merge 3 commits into
mainfrom
mathias/zod-serializer
Draft

feat(zod): add Zod v4 schema serializer#82
mafredri wants to merge 3 commits into
mainfrom
mathias/zod-serializer

Conversation

@mafredri
Copy link
Copy Markdown
Member

New zod package that serializes the guts intermediate AST into Zod v4 schema declarations. Walks the same AST that the TypeScript serializer uses, so all type mappings, mutations, and overrides are reflected.

Handles: z.object(), z.enum(), .nullable(), .optional(), z.array(), z.record(), schema references, inline objects, intersections, operator types.

Use case: coder/agents-chat-action maintains hand-written Zod schemas that mirror codersdk Go types. These drift when the Go types change (CODAGT-507). This serializer enables codegen from the same source of truth.

out := zod.Serialize(ts)
// or filter to specific types:
out := zod.SerializeFilter(ts, func(name string) bool {
    return wanted[name]
})

🤖 This PR was created with the help of Coder Agents, and will be reviewed by a human. 🏂🏻

Walks the same intermediate AST that the TypeScript serializer
uses. All type mappings, mutations, and overrides applied via
the guts pipeline are reflected in the Zod output.

Handles interfaces as z.object(), string-literal unions as
z.enum(), pointers as .nullable(), omitempty as .optional(),
arrays, records, references between schemas, inline object
literals, intersections, and operator types (readonly).
@mafredri
Copy link
Copy Markdown
Member Author

@Emyrk my agent did this unprompted. Not sure about adding it to guts but it could potentially be an example? Feel free to close or ask for follow-up. 👍🏻

@Emyrk
Copy link
Copy Markdown
Member

Emyrk commented May 28, 2026

@mafredri can I speak to your agent? Lol

I would like it to add a test so I can see the output

Copy link
Copy Markdown
Member Author

mafredri commented May 28, 2026

Hey! The agent is at https://dev.coder.com/agents/58b033ba-54bb-4f98-84a4-8264b7f02dd9 if you want to chat with it directly.

Added an end-to-end test that runs real Go structs through the full pipeline. Check testdata/zod/types.go for the input and testdata/zod/golden.ts for the output.

Run it with go test ./zod/ -run TestSerializeEndToEnd -v or update the golden with -update.

Also added heritage support (.extend() for embedded structs) and self-referential types (z.lazy()) which were missing from the initial commit.

mafredri added 2 commits May 28, 2026 13:11
Adds a testdata/zod package with realistic types (enums,
embedded structs, nullable pointers, arrays, maps, self-
referencing children) and a golden-file test that runs
the full guts pipeline through the Zod serializer.

Heritage clauses now emit .extend() for struct embedding.
Self-referential types emit z.lazy() to avoid reference-
before-declaration errors. The selfName parameter is
threaded through exprToZod to detect self-references
without global mutable state.
Adds a testdata/zod package with realistic types (enums,
embedded structs, nullable pointers, arrays, maps, self-
referencing children) and a golden-file test that runs
the full guts pipeline through the Zod serializer.

Heritage clauses now emit .extend() for struct embedding.
Self-referential types emit z.lazy() to avoid reference-
before-declaration errors. The selfName parameter is
threaded through exprToZod to detect self-references
without global mutable state.
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.

2 participants