Skip to content

Commit df8187b

Browse files
committed
Migrating some common helpers.
1 parent 8899422 commit df8187b

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { $ZodType } from "@zod/core";
1+
import type { $ZodObject, $ZodTransform, $ZodType } from "@zod/core";
22
import { Request } from "express";
33
import * as R from "ramda";
44
import { globalRegistry, z } from "zod";
@@ -86,11 +86,10 @@ export const getMessageFromError = (error: Error): string => {
8686
};
8787

8888
/** Takes the original unvalidated examples from the properties of ZodObject schema shape */
89-
export const pullExampleProps = <T extends z.ZodObject>(subject: T) =>
90-
Object.entries(subject.shape).reduce<Partial<z.input<T>>[]>(
89+
export const pullExampleProps = <T extends $ZodObject>(subject: T) =>
90+
Object.entries(subject._zod.def.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,
@@ -160,8 +159,8 @@ export const makeCleanId = (...args: string[]) => {
160159
};
161160

162161
export const getTransformedType = R.tryCatch(
163-
<T>(schema: z.ZodTransform<unknown, T>, sample: T) =>
164-
typeof schema.parse(sample),
162+
<T>(schema: $ZodTransform<unknown, T>, sample: T) =>
163+
typeof z.parse(schema, sample),
165164
R.always(undefined),
166165
);
167166

0 commit comments

Comments
 (0)