Skip to content

Commit e15e2fe

Browse files
authored
Migrating traverse related methods to core types (#2579)
Instead of #2574
1 parent a527948 commit e15e2fe

3 files changed

Lines changed: 14 additions & 11 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ export const getMessageFromError = (error: Error): string => {
8989
export const pullExampleProps = <T extends z.ZodObject>(subject: T) =>
9090
Object.entries(subject.shape).reduce<Partial<z.input<T>>[]>(
9191
(acc, [key, schema]) => {
92-
const examples =
93-
(schema as z.ZodType).meta()?.[metaSymbol]?.examples || [];
92+
const { examples = [] } = globalRegistry.get(schema)?.[metaSymbol] || {};
9493
return combinations(acc, examples.map(R.objOf(key)), ([left, right]) => ({
9594
...left,
9695
...right,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,9 @@ export const depictRequestParams = ({
404404
name,
405405
in: location,
406406
deprecated: globalRegistry.get(paramSchema)?.deprecated,
407-
required: !(paramSchema as z.ZodType).isOptional(),
407+
required: !(
408+
paramSchema instanceof z.ZodType && paramSchema.isOptional()
409+
),
408410
description: depicted.description || description,
409411
schema: result,
410412
examples: depictParamExamples(objectSchema, name),

express-zod-api/src/zts.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import type {
44
$ZodDefault,
55
$ZodDiscriminatedUnion,
66
$ZodEnum,
7+
$ZodInterface,
78
$ZodIntersection,
89
$ZodLazy,
910
$ZodLiteral,
1011
$ZodNullable,
12+
$ZodObject,
1113
$ZodOptional,
1214
$ZodPipe,
1315
$ZodReadonly,
@@ -66,7 +68,7 @@ const onLiteral: Producer = ({ _zod: { def } }: $ZodLiteral) => {
6668
return values.length === 1 ? values[0] : f.createUnionTypeNode(values);
6769
};
6870

69-
const onInterface: Producer = (int: z.ZodInterface, { next, makeAlias }) =>
71+
const onInterface: Producer = (int: $ZodInterface, { next, makeAlias }) =>
7072
makeAlias(int, () => {
7173
const members = Object.entries(int._zod.def.shape).map<ts.TypeElement>(
7274
([key, value]) => {
@@ -84,16 +86,16 @@ const onInterface: Producer = (int: z.ZodInterface, { next, makeAlias }) =>
8486
});
8587

8688
const onObject: Producer = (
87-
{ _zod: { def } }: z.ZodObject,
89+
{ _zod: { def } }: $ZodObject,
8890
{ isResponse, next },
8991
) => {
9092
const members = Object.entries(def.shape).map<ts.TypeElement>(
9193
([key, value]) => {
9294
const isOptional = isResponse
93-
? value instanceof z.ZodOptional
94-
: value instanceof z.ZodPromise
95-
? false
96-
: (value as z.ZodType).isOptional();
95+
? value._zod.def.type === "optional"
96+
: value._zod.def.type !== "promise" &&
97+
value instanceof z.ZodType &&
98+
value.isOptional();
9799
const { description: comment, deprecated: isDeprecated } =
98100
globalRegistry.get(value) || {};
99101
return makeInterfaceProp(key, next(value), {
@@ -211,9 +213,9 @@ const onFile: Producer = (schema: FileSchema) => {
211213
const stringType = ensureTypeNode(ts.SyntaxKind.StringKeyword);
212214
const bufferType = ensureTypeNode("Buffer");
213215
const unionType = f.createUnionTypeNode([stringType, bufferType]);
214-
return schema instanceof z.ZodString
216+
return schema._zod.def.type === "string"
215217
? stringType
216-
: schema instanceof z.ZodUnion
218+
: schema._zod.def.type === "union"
217219
? unionType
218220
: bufferType;
219221
};

0 commit comments

Comments
 (0)