Repository navigation
feat(zod): emit typed schemas for Set and Map collections (#451) - #535
Open
Utkarsh Patwa (utkarshpatwa393-sys) wants to merge 2 commits into
Open
Utkarsh Patwa (utkarshpatwa393-sys) wants to merge 2 commits into
Utkarsh Patwa (utkarshpatwa393-sys) wants to merge 2 commits into
Conversation
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>
Contributor
There was a problem hiding this comment.
🟡 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()asT[]andz.map()as[K, V][]instead ofany. - 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); |
There was a problem hiding this comment.
Addressed in commit f2bc01e:
- Bare collection schemas
z.set()andz.map()now emit their true TypeScript types (Set<T>andMap<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))andz.array(z.tuple(...)).transform(v => new Map(v))), verifying wire schema generation (string[],[string, number][]), JSON validation intoSet/Mapinstances, and full translation execution withcreateJsonTranslator.
…nsform regression tests Signed-off-by: Utkarsh patwa <utkarshpatwa393@gmail.com>
| } | ||
|
|
||
| function appendSetType(setType: z.ZodType) { | ||
| append("Set<"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Fixes #451 by emitting typed schemas for
z.set()andz.map()ingetZodSchemaAsTypeScriptinstead of falling through toany, and adding explicit regression tests for JSON-to-collection transformations.Context & Motivation
When generating prompt schemas with
getZodSchemaAsTypeScript,z.set()andz.map()previously lacked explicit handlers in the type switch and fell through toany.This PR provides:
Set<T>andMap<K, V>in TypeScript schemas matching standard library declarations.z.array(...).transform(v => new Set(v))andz.array(z.tuple(...)).transform(v => new Map(v)), the wire schema emits JSON-compatible arrays (T[]and[K, V][]) while validation cleanly hydrates runtimeSetandMapinstances.Changes
typescript/src/zod/validate.ts:case "set":to emitSet<T>.case "map":to emitMap<K, V>.typescript/test/zod.test.ts:z.set()andz.map()schema generation, including union types, verified with TypeScript compilation viaassertSchemasCompile.z.array(...).transform(...)wire schema emission and validation hydration intoSetandMapinstances.createJsonTranslatorand 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).