MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / formatZodValidationError

Function formatZodValidationError

source code/utils/toolErrors.ts:68–134  ·  view source on GitHub ↗
(
  toolName: string,
  error: ZodError,
)

Source from the content-addressed store, hash-verified

66 * @returns A formatted error message string
67 */
68export function formatZodValidationError(
69 toolName: string,
70 error: ZodError,
71): string {
72 const missingParams = error.issues
73 .filter(
74 err =>
75 err.code === 'invalid_type' &&
76 err.message.includes('received undefined'),
77 )
78 .map(err => formatValidationPath(err.path))
79
80 const unexpectedParams = error.issues
81 .filter(err => err.code === 'unrecognized_keys')
82 .flatMap(err => err.keys)
83
84 const typeMismatchParams = error.issues
85 .filter(
86 err =>
87 err.code === 'invalid_type' &&
88 !err.message.includes('received undefined'),
89 )
90 .map(err => {
91 const typeErr = err as { expected: string }
92 const receivedMatch = err.message.match(/received (\w+)/)
93 const received = receivedMatch ? receivedMatch[1] : 'unknown'
94 return {
95 param: formatValidationPath(err.path),
96 expected: typeErr.expected,
97 received,
98 }
99 })
100
101 // Default to original error message if we can't create a better one
102 let errorContent = error.message
103
104 // Build a human-readable error message
105 const errorParts = []
106
107 if (missingParams.length > 0) {
108 const missingParamErrors = missingParams.map(
109 param => `The required parameter \`${param}\` is missing`,
110 )
111 errorParts.push(...missingParamErrors)
112 }
113
114 if (unexpectedParams.length > 0) {
115 const unexpectedParamErrors = unexpectedParams.map(
116 param => `An unexpected parameter \`${param}\` was provided`,
117 )
118 errorParts.push(...unexpectedParamErrors)
119 }
120
121 if (typeMismatchParams.length > 0) {
122 const typeErrors = typeMismatchParams.map(
123 ({ param, expected, received }) =>
124 `The parameter \`${param}\` type is expected as \`${expected}\` but provided as \`${received}\``,
125 )

Callers 1

Calls 1

formatValidationPathFunction · 0.85

Tested by

no test coverage detected