Skip to content

Commit 694aa3e

Browse files
authored
Removing overrides for enums and literals (#2557)
`type` is not needed when you have `const` or `enum` entries. So this one can be fully delegated to Zod 4.
1 parent afd4737 commit 694aa3e

4 files changed

Lines changed: 1 addition & 175 deletions

File tree

example/example.documentation.yaml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ paths:
3030
properties:
3131
status:
3232
const: success
33-
type: string
3433
data:
3534
type: object
3635
properties:
@@ -59,7 +58,6 @@ paths:
5958
properties:
6059
status:
6160
const: error
62-
type: string
6361
error:
6462
type: object
6563
properties:
@@ -177,7 +175,6 @@ paths:
177175
properties:
178176
status:
179177
const: success
180-
type: string
181178
data:
182179
type: object
183180
properties:
@@ -215,7 +212,6 @@ paths:
215212
properties:
216213
status:
217214
const: error
218-
type: string
219215
error:
220216
type: object
221217
properties:
@@ -260,7 +256,6 @@ paths:
260256
properties:
261257
status:
262258
const: created
263-
type: string
264259
data:
265260
type: object
266261
properties:
@@ -281,7 +276,6 @@ paths:
281276
properties:
282277
status:
283278
const: created
284-
type: string
285279
data:
286280
type: object
287281
properties:
@@ -302,7 +296,6 @@ paths:
302296
properties:
303297
status:
304298
const: error
305-
type: string
306299
reason:
307300
type: string
308301
required:
@@ -317,7 +310,6 @@ paths:
317310
properties:
318311
status:
319312
const: exists
320-
type: string
321313
id:
322314
type: integer
323315
exclusiveMinimum: -9007199254740991
@@ -334,7 +326,6 @@ paths:
334326
properties:
335327
status:
336328
const: error
337-
type: string
338329
reason:
339330
type: string
340331
required:
@@ -465,7 +456,6 @@ paths:
465456
properties:
466457
status:
467458
const: success
468-
type: string
469459
data:
470460
type: object
471461
properties:
@@ -502,7 +492,6 @@ paths:
502492
properties:
503493
status:
504494
const: error
505-
type: string
506495
error:
507496
type: object
508497
properties:
@@ -542,7 +531,6 @@ paths:
542531
properties:
543532
status:
544533
const: success
545-
type: string
546534
data:
547535
type: object
548536
properties:
@@ -563,7 +551,6 @@ paths:
563551
properties:
564552
status:
565553
const: error
566-
type: string
567554
error:
568555
type: object
569556
properties:
@@ -608,7 +595,6 @@ paths:
608595
exclusiveMaximum: 9007199254740991
609596
event:
610597
const: time
611-
type: string
612598
id:
613599
type: string
614600
retry:
@@ -660,7 +646,6 @@ paths:
660646
properties:
661647
status:
662648
const: success
663-
type: string
664649
data:
665650
type: object
666651
properties:
@@ -681,7 +666,6 @@ paths:
681666
properties:
682667
status:
683668
const: error
684-
type: string
685669
error:
686670
type: object
687671
properties:

express-zod-api/src/documentation-helpers.ts

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import type {
2-
$ZodEnum,
3-
$ZodLiteral,
4-
$ZodPipe,
5-
$ZodTuple,
6-
$ZodType,
7-
JSONSchema,
8-
} from "@zod/core";
1+
import type { $ZodPipe, $ZodTuple, $ZodType, JSONSchema } from "@zod/core";
92
import {
103
ExamplesObject,
114
isReferenceObject,
@@ -200,25 +193,6 @@ const onNullable: Overrider = ({ jsonSchema }) => {
200193
const isSupportedType = (subject: string): subject is SchemaObjectType =>
201194
subject in samples;
202195

203-
const getSupportedType = (value: unknown): SchemaObjectType | undefined => {
204-
const detected = R.toLower(R.type(value)); // toLower is typed well unlike .toLowerCase()
205-
return typeof value === "bigint"
206-
? "integer"
207-
: isSupportedType(detected)
208-
? detected
209-
: undefined;
210-
};
211-
212-
const onEnum: Overrider = ({ zodSchema, jsonSchema }) =>
213-
(jsonSchema.type = getSupportedType(
214-
Object.values((zodSchema as $ZodEnum)._zod.def.entries)[0],
215-
));
216-
217-
const onLiteral: Overrider = ({ zodSchema, jsonSchema }) =>
218-
(jsonSchema.type = getSupportedType(
219-
Object.values((zodSchema as $ZodLiteral)._zod.def.values)[0],
220-
));
221-
222196
const onDateIn: Overrider = ({ jsonSchema }, ctx) => {
223197
if (ctx.isResponse)
224198
throw new DocumentationError("Please use ez.dateOut() for output.", ctx);
@@ -443,10 +417,8 @@ const overrides: Partial<Record<FirstPartyKind | ProprietaryBrand, Overrider>> =
443417
default: onDefault,
444418
any: onAny,
445419
union: onUnion,
446-
enum: onEnum,
447420
bigint: onBigInt,
448421
intersection: onIntersection,
449-
literal: onLiteral,
450422
tuple: onTuple,
451423
pipe: onPipeline,
452424
[ezDateInBrand]: onDateIn,

express-zod-api/tests/__snapshots__/documentation-helpers.spec.ts.snap

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ exports[`Documentation helpers > depictEnum() > should set type and enum propert
139139
"one",
140140
"two",
141141
],
142-
"type": "string",
143142
}
144143
`;
145144

@@ -149,7 +148,6 @@ exports[`Documentation helpers > depictEnum() > should set type and enum propert
149148
"ONE",
150149
"TWO",
151150
],
152-
"type": "string",
153151
}
154152
`;
155153

@@ -288,7 +286,6 @@ exports[`Documentation helpers > depictIntersection() > should fall back to allO
288286
},
289287
{
290288
"const": 5,
291-
"type": "number",
292289
},
293290
],
294291
}
@@ -411,28 +408,24 @@ exports[`Documentation helpers > depictLiteral() > should handle multiple values
411408
2,
412409
3,
413410
],
414-
"type": "number",
415411
}
416412
`;
417413

418414
exports[`Documentation helpers > depictLiteral() > should set type and involve const property 0 1`] = `
419415
{
420416
"const": "testng",
421-
"type": "string",
422417
}
423418
`;
424419

425420
exports[`Documentation helpers > depictLiteral() > should set type and involve const property 1 1`] = `
426421
{
427422
"const": null,
428-
"type": "null",
429423
}
430424
`;
431425

432426
exports[`Documentation helpers > depictLiteral() > should set type and involve const property 2 1`] = `
433427
{
434428
"const": 123,
435-
"type": "integer",
436429
}
437430
`;
438431

@@ -739,7 +732,6 @@ exports[`Documentation helpers > depictRecord() > should set properties+required
739732
"one",
740733
"two",
741734
],
742-
"type": "string",
743735
},
744736
"type": "object",
745737
}
@@ -752,7 +744,6 @@ exports[`Documentation helpers > depictRecord() > should set properties+required
752744
},
753745
"propertyNames": {
754746
"const": "testing",
755-
"type": "string",
756747
},
757748
"type": "object",
758749
}
@@ -767,11 +758,9 @@ exports[`Documentation helpers > depictRecord() > should set properties+required
767758
"anyOf": [
768759
{
769760
"const": "one",
770-
"type": "string",
771761
},
772762
{
773763
"const": "two",
774-
"type": "string",
775764
},
776765
],
777766
},
@@ -1327,7 +1316,6 @@ exports[`Documentation helpers > depictTuple() > should utilize prefixItems and
13271316
},
13281317
{
13291318
"const": "test",
1330-
"type": "string",
13311319
},
13321320
],
13331321
"type": "array",
@@ -1344,7 +1332,6 @@ exports[`Documentation helpers > depictUnion() > should wrap next depicters in o
13441332
},
13451333
"status": {
13461334
"const": "success",
1347-
"type": "string",
13481335
},
13491336
},
13501337
"required": [
@@ -1368,7 +1355,6 @@ exports[`Documentation helpers > depictUnion() > should wrap next depicters in o
13681355
},
13691356
"status": {
13701357
"const": "error",
1371-
"type": "string",
13721358
},
13731359
},
13741360
"required": [
@@ -1519,7 +1505,6 @@ exports[`Documentation helpers > excludeParamsFromDepiction() > should omit spec
15191505
},
15201506
"propertyNames": {
15211507
"const": "a",
1522-
"type": "string",
15231508
},
15241509
"type": "object",
15251510
},

0 commit comments

Comments
 (0)