Skip to content

feat(zod): emit typed schemas for Set and Map collections (#451) - #535

Open
Utkarsh Patwa (utkarshpatwa393-sys) wants to merge 2 commits into
microsoft:mainfrom
utkarshpatwa393-sys:feat/zod-collection-schemas-451
Open

Utkarsh Patwa (utkarshpatwa393-sys) wants to merge 2 commits into
microsoft:mainfrom
utkarshpatwa393-sys:feat/zod-collection-schemas-451

Conversation

@utkarshpatwa393-sys

@utkarshpatwa393-sys Utkarsh Patwa (utkarshpatwa393-sys) commented Nov 19, 2026 •

Copy link
Copy Markdown

Fixes #451 by emitting typed schemas for z.set() and z.map() in getZodSchemaAsTypeScript instead of falling through to any, and adding explicit regression tests for JSON-to-collection transformations.

Context & Motivation

When generating prompt schemas with getZodSchemaAsTypeScript, z.set() and z.map() previously lacked explicit handlers in the type switch and fell through to any.

This PR provides:

  1. Direct collection schema emission: Set<T> and Map<K, V> in TypeScript schemas matching standard library declarations.
  2. Verified explicit wire-to-runtime transforms: in TypeChat applications using z.array(...).transform(v => new Set(v)) and z.array(z.tuple(...)).transform(v => new Map(v)), the wire schema emits JSON-compatible arrays (T[] and [K, V][]) while validation cleanly hydrates runtime Set and Map instances.

Changes

  • typescript/src/zod/validate.ts:
    • Added case "set": to emit Set<T>.
    • Added case "map": to emit Map<K, V>.
  • typescript/test/zod.test.ts:
    • Added unit tests for z.set() and z.map() schema generation, including union types, verified with TypeScript compilation via assertSchemasCompile.
    • Added regression tests for explicit z.array(...).transform(...) wire schema emission and validation hydration into Set and Map instances.
    • Added end-to-end translation test with createJsonTranslator and a stubbed model.

Validation

  • npm run build: Clean build of both CJS and ESM distributions.
  • npm test: 377 tests passed, 0 failed (67 test suites).

Zod `z.set()` and `z.map()` previously fell through to `any` in
`getZodSchemaAsTypeScript`.

This change:
- Emits `T[]` for `z.set()` as the JSON-compatible wire schema.
- Emits `[K, V][]` for `z.map()` as the key-value tuple array wire schema.
- Adds test coverage for `z.set()` and `z.map()` in `test/zod.test.ts`,
  including union elements.

Closes microsoft#451

Signed-off-by: Utkarsh patwa <utkarshpatwa393@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 09:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Bare z.set() and z.map() schemas reject the translator’s plain JSON arrays, causing even schema-conforming responses to fail validation with no repair path to the required collection instances.

1 open issue
What changed in this PR

This PR adds explicit collection representations to TypeChat’s Zod prompt-schema emitter, addressing the schema-generation portion of #451.

Changes:

  • Emits z.set() as T[] and z.map() as [K, V][] instead of any.
  • Adds collection-shape and union-handling tests whose generated declarations use the existing TypeScript compilation checks.
File Description
typescript/​test/​zod.test.ts Adds Set and Map schema-output tests, including union members.
typescript/​src/​zod/​validate.ts Adds Set and Map handlers that emit array-based prompt types.

Review effort: Balanced

Comment on lines +166 to +169
case "set":
return appendSetType(type);
case "map":
return appendMapType(type);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Addressed in commit f2bc01e:

  • Bare collection schemas z.set() and z.map() now emit their true TypeScript types (Set<T> and Map<K, V>) rather than advertising raw array wire schemas that bare Zod collection instances reject.
  • Added comprehensive regression tests for explicit JSON-to-collection transforms (z.array(...).transform(v => new Set(v)) and z.array(z.tuple(...)).transform(v => new Map(v))), verifying wire schema generation (string[], [string, number][]), JSON validation into Set/Map instances, and full translation execution with createJsonTranslator.

…nsform regression tests

Signed-off-by: Utkarsh patwa <utkarshpatwa393@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 09:57
@utkarshpatwa393-sys Utkarsh Patwa (utkarshpatwa393-sys) changed the title feat(zod): emit typed wire schemas for Set and Map collections (#451) feat(zod): emit typed schemas for Set and Map collections (#451) Nov 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Caller-supplied schema names can shadow the generated Set and Map references, causing TypeScript compilation failures unless both references are qualified with globalThis.

2 open issues

Review effort: Balanced

}

function appendSetType(setType: z.ZodType) {
append("Set<");
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.

Support explicit JSON-to-runtime mappings with accurate prompt schemas

2 participants