Skip to content

Commit 3b1dac3

Browse files
authored
fix(zod): apply propertyNames to string schemas in JSON Schema gen (#217)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Tests** - Introduced an edge-case test to verify proper handling of schemas with date-formatted keys. - **Refactor** - Streamlined the logic for processing record key types, simplifying the conversion of schema definitions. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 9adcd05 commit 3b1dac3

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

packages/zod/src/converter.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,11 @@ const edgeCases: SchemaTestCase[] = [
405405
input: [true, { type: 'object', additionalProperties: { type: 'string' }, propertyNames: { type: 'number' } }],
406406
ignoreZodToJsonSchema: true,
407407
},
408+
{
409+
schema: z.record(z.string().date(), z.string()),
410+
input: [true, { type: 'object', additionalProperties: { type: 'string' }, propertyNames: { type: 'string', format: 'date' } }],
411+
ignoreZodToJsonSchema: true,
412+
},
408413
]
409414

410415
describe.each([

packages/zod/src/converter.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,9 @@ export class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
413413

414414
const json: JSONSchema = { type: 'object' }
415415

416-
const keyTypeName = this.#getZodTypeName(schema_._def.keyType._def)
416+
const [__, keyJson] = this.convert(schema_._def.keyType, options, lazyDepth, false, false)
417417

418-
if (keyTypeName !== ZodFirstPartyTypeKind.ZodString) {
419-
const [_, keyJson] = this.convert(schema_._def.keyType, options, lazyDepth, false, false)
418+
if (Object.entries(keyJson).some(([k, v]) => k !== 'type' || v !== 'string')) {
420419
json.propertyNames = keyJson
421420
}
422421

0 commit comments

Comments
 (0)