Skip to content

Commit 9e66ffd

Browse files
committed
Ref: flipping condition in pipeline overrider and producer.
1 parent fbd5bf3 commit 9e66ffd

2 files changed

Lines changed: 32 additions & 38 deletions

File tree

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -271,28 +271,24 @@ export const onPipeline: Overrider = ({ zodSchema, jsonSchema }, ctx) => {
271271
const opposite = (zodSchema as $ZodPipe)._zod.def[
272272
ctx.isResponse ? "in" : "out"
273273
];
274-
if (isSchema<$ZodTransform>(target, "transform")) {
275-
const opposingDepiction = depict(opposite, { ctx });
276-
if (isSchemaObject(opposingDepiction)) {
277-
if (!ctx.isResponse) {
278-
const { type: opposingType, ...rest } = opposingDepiction;
274+
if (!isSchema<$ZodTransform>(target, "transform")) return;
275+
const opposingDepiction = depict(opposite, { ctx });
276+
if (isSchemaObject(opposingDepiction)) {
277+
if (!ctx.isResponse) {
278+
const { type: opposingType, ...rest } = opposingDepiction;
279+
Object.assign(jsonSchema, {
280+
...rest,
281+
format: `${rest.format || opposingType} (preprocessed)`,
282+
});
283+
} else {
284+
const targetType = getTransformedType(
285+
target,
286+
makeSample(opposingDepiction),
287+
);
288+
if (targetType && ["number", "string", "boolean"].includes(targetType)) {
279289
Object.assign(jsonSchema, {
280-
...rest,
281-
format: `${rest.format || opposingType} (preprocessed)`,
290+
type: targetType as "number" | "string" | "boolean",
282291
});
283-
} else {
284-
const targetType = getTransformedType(
285-
target,
286-
makeSample(opposingDepiction),
287-
);
288-
if (
289-
targetType &&
290-
["number", "string", "boolean"].includes(targetType)
291-
) {
292-
Object.assign(jsonSchema, {
293-
type: targetType as "number" | "string" | "boolean",
294-
});
295-
}
296292
}
297293
}
298294
}

express-zod-api/src/zts.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -184,24 +184,22 @@ const onPipeline: Producer = (
184184
) => {
185185
const target = def[isResponse ? "out" : "in"];
186186
const opposite = def[isResponse ? "in" : "out"];
187-
if (isSchema<$ZodTransform>(target, "transform")) {
188-
const opposingType = next(opposite);
189-
const targetType = getTransformedType(target, makeSample(opposingType));
190-
const resolutions: Partial<
191-
Record<NonNullable<typeof targetType>, ts.KeywordTypeSyntaxKind>
192-
> = {
193-
number: ts.SyntaxKind.NumberKeyword,
194-
bigint: ts.SyntaxKind.BigIntKeyword,
195-
boolean: ts.SyntaxKind.BooleanKeyword,
196-
string: ts.SyntaxKind.StringKeyword,
197-
undefined: ts.SyntaxKind.UndefinedKeyword,
198-
object: ts.SyntaxKind.ObjectKeyword,
199-
};
200-
return ensureTypeNode(
201-
(targetType && resolutions[targetType]) || ts.SyntaxKind.AnyKeyword,
202-
);
203-
}
204-
return next(target);
187+
if (!isSchema<$ZodTransform>(target, "transform")) return next(target);
188+
const opposingType = next(opposite);
189+
const targetType = getTransformedType(target, makeSample(opposingType));
190+
const resolutions: Partial<
191+
Record<NonNullable<typeof targetType>, ts.KeywordTypeSyntaxKind>
192+
> = {
193+
number: ts.SyntaxKind.NumberKeyword,
194+
bigint: ts.SyntaxKind.BigIntKeyword,
195+
boolean: ts.SyntaxKind.BooleanKeyword,
196+
string: ts.SyntaxKind.StringKeyword,
197+
undefined: ts.SyntaxKind.UndefinedKeyword,
198+
object: ts.SyntaxKind.ObjectKeyword,
199+
};
200+
return ensureTypeNode(
201+
(targetType && resolutions[targetType]) || ts.SyntaxKind.AnyKeyword,
202+
);
205203
};
206204

207205
const onNull: Producer = () => makeLiteralType(null);

0 commit comments

Comments
 (0)