Skip to content

Commit 7ce0866

Browse files
committed
Rev: preserving depictEnum and depictLiteral fallback for backward compatibility.
1 parent 3405c20 commit 7ce0866

4 files changed

Lines changed: 74 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
### v24.2.1
66

7-
- Removed overrides for depicting enums in the generated `Documentation`:
8-
- The better implementation provided by [Zod v3.25.45](https://github.com/colinhacks/zod/releases/tag/v3.25.45);
9-
- Removed overrides for depicting literals in the generated `Documentation`:
10-
- Fixed in [Zod v3.25.49](https://github.com/colinhacks/zod/releases/tag/v3.25.49).
7+
- Prioritizing Zod native depiction for `z.enum()` and `z.literal()` by the `Documentation` generator:
8+
- Enum `type` implemented in [Zod v3.25.45](https://github.com/colinhacks/zod/releases/tag/v3.25.45);
9+
- Literal `type` implemented in [Zod v3.25.49](https://github.com/colinhacks/zod/releases/tag/v3.25.49).
1110

1211
### v24.2.0
1312

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,24 @@ export const depictNullable: Depicter = ({ jsonSchema }) => {
143143
const isSupportedType = (subject: string): subject is SchemaObjectType =>
144144
subject in samples;
145145

146+
/**
147+
* @todo remove in v25
148+
* @since zod 3.25.45
149+
*/
150+
export const depictEnum: Depicter = ({ jsonSchema }) => {
151+
jsonSchema.type ??= typeof jsonSchema.enum?.[0];
152+
return jsonSchema;
153+
};
154+
155+
/**
156+
* @todo remove in v25
157+
* @since zod 3.25.49
158+
* */
159+
export const depictLiteral: Depicter = ({ jsonSchema }) => {
160+
jsonSchema.type ??= typeof (jsonSchema.const || jsonSchema.enum?.[0]);
161+
return jsonSchema;
162+
};
163+
146164
const ensureCompliance = ({
147165
$ref,
148166
type,
@@ -377,6 +395,8 @@ const depicters: Partial<Record<FirstPartyKind | ProprietaryBrand, Depicter>> =
377395
intersection: depictIntersection,
378396
tuple: depictTuple,
379397
pipe: depictPipeline,
398+
literal: depictLiteral,
399+
enum: depictEnum,
380400
[ezDateInBrand]: depictDateIn,
381401
[ezDateOutBrand]: depictDateOut,
382402
[ezUploadBrand]: depictUpload,

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ DocumentationError({
106106
})
107107
`;
108108

109+
exports[`Documentation helpers > depictEnum() > should set type 1`] = `
110+
{
111+
"enum": [
112+
"test",
113+
"jest",
114+
],
115+
"type": "string",
116+
}
117+
`;
118+
109119
exports[`Documentation helpers > depictIntersection() > should NOT flatten object schemas having conflicting props 1`] = `
110120
{
111121
"allOf": [
@@ -190,6 +200,23 @@ exports[`Documentation helpers > depictIntersection() > should merge examples de
190200
}
191201
`;
192202

203+
exports[`Documentation helpers > depictLiteral() > should set type from either const or enum prop 0 1`] = `
204+
{
205+
"const": "test",
206+
"type": "string",
207+
}
208+
`;
209+
210+
exports[`Documentation helpers > depictLiteral() > should set type from either const or enum prop 1 1`] = `
211+
{
212+
"enum": [
213+
"test",
214+
"jest",
215+
],
216+
"type": "string",
217+
}
218+
`;
219+
193220
exports[`Documentation helpers > depictNullable() > should add null type to the first of anyOf 0 1`] = `
194221
{
195222
"type": [

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import {
2525
depictDateIn,
2626
depictDateOut,
2727
depictBody,
28+
depictEnum,
29+
depictLiteral,
2830
depictRequest,
2931
} from "../src/documentation-helpers";
3032

@@ -301,6 +303,28 @@ describe("Documentation helpers", () => {
301303
});
302304
});
303305

306+
describe("depictEnum()", () => {
307+
test("should set type", () => {
308+
expect(
309+
depictEnum(
310+
{ zodSchema: z.never(), jsonSchema: { enum: ["test", "jest"] } },
311+
requestCtx,
312+
),
313+
).toMatchSnapshot();
314+
});
315+
});
316+
317+
describe("depictLiteral()", () => {
318+
test.each([{ const: "test" }, { enum: ["test", "jest"] }])(
319+
"should set type from either const or enum prop %#",
320+
(jsonSchema) => {
321+
expect(
322+
depictLiteral({ zodSchema: z.never(), jsonSchema }, requestCtx),
323+
).toMatchSnapshot();
324+
},
325+
);
326+
});
327+
304328
describe("depictBigInt()", () => {
305329
test("should set type:string and format:bigint", () => {
306330
expect(

0 commit comments

Comments
 (0)