Skip to content

Commit c95c8c8

Browse files
authored
Cleanup: rm depictObject (#2645)
Added in 9366d0f as part of #2595 for better treating optionals and required props. But it seems that stable Zod 4 does the good job without it.
1 parent d9625e6 commit c95c8c8

File tree

6 files changed

+5
-84
lines changed

6 files changed

+5
-84
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export const getTransformedType = R.tryCatch(
128128
R.always(undefined),
129129
);
130130

131+
/** @todo check usage */
131132
export const isOptional = (
132133
{ _zod: { optin, optout } }: $ZodType,
133134
{ isResponse }: { isResponse: boolean },

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type {
2-
$ZodObject,
32
$ZodPipe,
43
$ZodTransform,
54
$ZodTuple,
@@ -30,7 +29,6 @@ import {
3029
getRoutePathParams,
3130
getTransformedType,
3231
isObject,
33-
isOptional,
3432
isSchema,
3533
makeCleanId,
3634
routePathParamsRegex,
@@ -153,23 +151,6 @@ export const depictLiteral: Depicter = ({ jsonSchema }) => ({
153151
...jsonSchema,
154152
});
155153

156-
/** @todo might no longer be required */
157-
export const depictObject: Depicter = (
158-
{ zodSchema, jsonSchema },
159-
{ isResponse },
160-
) => {
161-
if (isResponse) return jsonSchema;
162-
if (!isSchema<$ZodObject>(zodSchema, "object")) return jsonSchema;
163-
const { required = [] } = jsonSchema as JSONSchema.ObjectSchema;
164-
const result: string[] = [];
165-
for (const key of required) {
166-
const valueSchema = zodSchema._zod.def.shape[key];
167-
if (valueSchema && !isOptional(valueSchema, { isResponse }))
168-
result.push(key);
169-
}
170-
return { ...jsonSchema, required: result };
171-
};
172-
173154
const ensureCompliance = ({
174155
$ref,
175156
type,
@@ -400,7 +381,6 @@ const depicters: Partial<Record<FirstPartyKind | ProprietaryBrand, Depicter>> =
400381
pipe: depictPipeline,
401382
literal: depictLiteral,
402383
enum: depictEnum,
403-
object: depictObject,
404384
[ezDateInBrand]: depictDateIn,
405385
[ezDateOutBrand]: depictDateOut,
406386
[ezUploadBrand]: depictUpload,

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

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -282,41 +282,6 @@ exports[`Documentation helpers > depictNullable() > should not add null type whe
282282
}
283283
`;
284284

285-
exports[`Documentation helpers > depictObject() > should remove optional props from required for request 0 1`] = `
286-
{
287-
"properties": {
288-
"a": {
289-
"type": "number",
290-
},
291-
"b": {
292-
"type": "string",
293-
},
294-
},
295-
"required": [
296-
"a",
297-
"b",
298-
],
299-
"type": "object",
300-
}
301-
`;
302-
303-
exports[`Documentation helpers > depictObject() > should remove optional props from required for request 1 1`] = `
304-
{
305-
"properties": {
306-
"a": {
307-
"type": "number",
308-
},
309-
"b": {
310-
"type": "string",
311-
},
312-
},
313-
"required": [
314-
"b",
315-
],
316-
"type": "object",
317-
}
318-
`;
319-
320285
exports[`Documentation helpers > depictPipeline > should depict as 'number (out)' 1`] = `
321286
{
322287
"type": "number",

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,8 @@ paths:
14961496
type: integer
14971497
minimum: 0
14981498
maximum: 0
1499+
coercedNum:
1500+
type: number
14991501
required:
15001502
- double
15011503
- doublePositive
@@ -1505,6 +1507,7 @@ paths:
15051507
- intPositive
15061508
- intNegative
15071509
- intLimited
1510+
- coercedNum
15081511
required: true
15091512
responses:
15101513
"200":

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

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
depictEnum,
2929
depictLiteral,
3030
depictRequest,
31-
depictObject,
3231
} from "../src/documentation-helpers";
3332

3433
describe("Documentation helpers", () => {
@@ -332,34 +331,6 @@ describe("Documentation helpers", () => {
332331
);
333332
});
334333

335-
describe("depictObject()", () => {
336-
test.each([
337-
{
338-
zodSchema: z.object({ a: z.number(), b: z.string() }),
339-
jsonSchema: {
340-
type: "object",
341-
properties: { a: { type: "number" }, b: { type: "string" } },
342-
required: ["a", "b"],
343-
},
344-
},
345-
{
346-
zodSchema: z.object({ a: z.number().optional(), b: z.coerce.string() }),
347-
jsonSchema: {
348-
type: "object",
349-
properties: { a: { type: "number" }, b: { type: "string" } },
350-
required: ["b"], // Zod 4: coerce remains
351-
},
352-
},
353-
])(
354-
"should remove optional props from required for request %#",
355-
({ zodSchema, jsonSchema }) => {
356-
expect(
357-
depictObject({ zodSchema, jsonSchema }, requestCtx),
358-
).toMatchSnapshot();
359-
},
360-
);
361-
});
362-
363334
describe("depictBigInt()", () => {
364335
test("should set type:string and format:bigint", () => {
365336
expect(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ describe("Documentation", () => {
330330
intNegative: z.int().negative(),
331331
intLimited: z.int().min(-100).max(100),
332332
zero: z.int().nonnegative().nonpositive().optional(),
333+
coercedNum: z.coerce.number(), // required prop in zod 4
333334
}),
334335
output: z.object({
335336
bigint: z.bigint(),

0 commit comments

Comments
 (0)