MCPcopy
hub / github.com/ts-rest/ts-rest / checkZodSchema

Function checkZodSchema

libs/ts-rest/core/src/lib/zod-utils.ts:57–93  ·  view source on GitHub ↗
(
  data: unknown,
  schema: unknown,
  { passThroughExtraKeys = false } = {},
)

Source from the content-addressed store, hash-verified

55};
56
57export const checkZodSchema = (
58 data: unknown,
59 schema: unknown,
60 { passThroughExtraKeys = false } = {},
61):
62 | {
63 success: true;
64 data: unknown;
65 }
66 | {
67 success: false;
68 error: z.ZodError;
69 } => {
70 if (isZodType(schema)) {
71 const result = schema.safeParse(data);
72
73 if (result.success) {
74 return {
75 success: true,
76 data:
77 passThroughExtraKeys && typeof data === 'object'
78 ? { ...data, ...result.data }
79 : result.data,
80 };
81 }
82
83 return {
84 success: false,
85 error: result.error,
86 };
87 }
88
89 return {
90 success: true,
91 data: data,
92 };
93};
94
95// Convert a ZodError to a plain object because ZodError extends Error and causes problems with NestJS
96export const zodErrorResponse = (

Callers 9

interceptMethod · 0.90
validateResponseFunction · 0.90
transformMethod · 0.90
validateRequestFunction · 0.90
validateRequestFunction · 0.90
zod-utils.spec.tsFile · 0.90
validateResponseFunction · 0.90
handlerFactoryFunction · 0.90
validateRequestFunction · 0.90

Calls 1

isZodTypeFunction · 0.85

Tested by

no test coverage detected