Skip to content

Commit 593329c

Browse files
authored
fix(zod): avoid trailing commas in additionalProperties consts (orval-labs#2911)
1 parent 9ef7809 commit 593329c

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

packages/zod/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,11 @@ ${Object.entries(mergedProperties)
861861
.map((prop: any) => parseProperty(prop))
862862
.join('');
863863
const valueWithZod = `${value.startsWith('.') ? 'zod' : ''}${value}`;
864-
consts += args.consts;
864+
if (typeof args.consts === 'string') {
865+
consts += args.consts;
866+
} else if (Array.isArray(args.consts)) {
867+
consts += args.consts.join('\n');
868+
}
865869
return `zod.record(zod.string(), ${valueWithZod})`;
866870
}
867871

packages/zod/src/zod.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ const additionalPropertiesSchema: OpenApiSchemaObject = {
141141
},
142142
};
143143

144+
const additionalPropertiesMaxLengthSchema: OpenApiSchemaObject = {
145+
type: 'object',
146+
additionalProperties: {
147+
type: 'string',
148+
maxLength: 253,
149+
},
150+
};
151+
144152
describe('generateZodValidationSchemaDefinition`', () => {
145153
it('required', () => {
146154
const result = generateZodValidationSchemaDefinition(
@@ -310,6 +318,43 @@ describe('generateZodValidationSchemaDefinition`', () => {
310318
});
311319
});
312320

321+
it('additionalProperties with maxLength', () => {
322+
const result = generateZodValidationSchemaDefinition(
323+
additionalPropertiesMaxLengthSchema,
324+
{
325+
output: {
326+
override: {
327+
useDates: false,
328+
},
329+
},
330+
} as ContextSpec,
331+
'additionalPropertiesMaxLength',
332+
true,
333+
false,
334+
{
335+
required: true,
336+
},
337+
);
338+
339+
const parsed = parseZodValidationSchemaDefinition(
340+
result,
341+
{
342+
output: {
343+
override: {
344+
useDates: false,
345+
},
346+
},
347+
} as ContextSpec,
348+
false,
349+
false,
350+
false,
351+
);
352+
353+
expect(parsed.zod).toContain('zod.record(zod.string(), zod.string().max(');
354+
expect(parsed.consts).toContain('= 253;');
355+
expect(parsed.consts).not.toContain(';,');
356+
});
357+
313358
it('handles allOf with base type string', () => {
314359
const stringWithConstraints: SchemaObject = {
315360
type: 'string',

0 commit comments

Comments
 (0)