Skip to content

Commit

Permalink
Fix exclusive max/min for openapi 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverzheng committed Dec 23, 2024
1 parent b357852 commit dee5469
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/zod-openapi/src/lib/zod-openapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ describe('zodOpenapi', () => {
aDate: z.date(),
aNullableString: z.string().nullable(),
aUnionIncludingNull: z.union([z.string(), z.null(), z.number()]),
aNumberMin: z.number().min(3).optional(),
aNumberGt: z.number().gt(5).optional(),
}),
{
description: `Primitives also testing overwriting of "required"`,
Expand All @@ -65,6 +67,8 @@ describe('zodOpenapi', () => {
aDate: { type: 'string', format: 'date-time' },
aNullableString: { type: 'string', nullable: true },
aUnionIncludingNull: { oneOf: [{ type: 'string' }, { type: 'number' }], nullable: true },
aNumberMin: { type: 'number', minimum: 3 },
aNumberGt: { type: 'number', minimum: 5, exclusiveMinimum: true },
},
required: ['aBigInt', 'aBoolean', 'aDate', 'aNullableString', 'aUnionIncludingNull', 'aNumber'],
description: 'Primitives also testing overwriting of "required"',
Expand Down
26 changes: 22 additions & 4 deletions packages/zod-openapi/src/lib/zod-openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,30 @@ function parseNumber({
checks.forEach((item) => {
switch (item.kind) {
case 'max':
if (item.inclusive) baseSchema.maximum = item.value;
else baseSchema.exclusiveMaximum = item.value;
if (item.inclusive || openApiVersion === '3.0') {
baseSchema.maximum = item.value;
}
if (!item.inclusive) {
if (openApiVersion === '3.0') {
// exclusiveMaximum has conflicting types in oas31 and oas30
baseSchema.exclusiveMaximum = true as unknown as number;
} else {
baseSchema.exclusiveMaximum = item.value;
}
}
break;
case 'min':
if (item.inclusive) baseSchema.minimum = item.value;
else baseSchema.exclusiveMinimum = item.value;
if (item.inclusive || openApiVersion === '3.0') {
baseSchema.minimum = item.value;
}
if (!item.inclusive) {
if (openApiVersion === '3.0') {
// exclusiveMinimum has conflicting types in oas31 and oas30
baseSchema.exclusiveMinimum = true as unknown as number;
} else {
baseSchema.exclusiveMinimum = item.value;
}
}
break;
case 'int':
baseSchema.type = typeFormat('integer', openApiVersion);
Expand Down

0 comments on commit dee5469

Please sign in to comment.