|
9 | 9 | } from '$promptl/parser/interfaces' |
10 | 10 | import { ContentTypeTagName, MessageRole } from '$promptl/types' |
11 | 11 | import { Scalar, Node as YAMLItem, YAMLMap, YAMLSeq } from 'yaml' |
12 | | -import { ZodError } from 'zod' |
| 12 | +import { ZodError, ZodIssue, ZodIssueCode } from 'zod' |
13 | 13 |
|
14 | 14 | export function isIterable(obj: unknown): obj is Iterable<unknown> { |
15 | 15 | return (obj as Iterable<unknown>)?.[Symbol.iterator] !== undefined |
@@ -109,3 +109,139 @@ export function isZodError(error: unknown): error is ZodError { |
109 | 109 |
|
110 | 110 | return false |
111 | 111 | } |
| 112 | + |
| 113 | +function collectAllLeafIssues(issue: ZodIssue): ZodIssue[] { |
| 114 | + switch (issue.code) { |
| 115 | + case ZodIssueCode.invalid_union: { |
| 116 | + // invalid_union.issue.unionErrors is ZodError[] |
| 117 | + const unionErrs: ZodError[] = (issue as any).unionErrors ?? [] |
| 118 | + return unionErrs.flatMap((nestedZodError) => |
| 119 | + nestedZodError.issues.flatMap((nestedIssue) => |
| 120 | + collectAllLeafIssues(nestedIssue), |
| 121 | + ), |
| 122 | + ) |
| 123 | + } |
| 124 | + |
| 125 | + case ZodIssueCode.invalid_arguments: { |
| 126 | + // invalid_arguments.issue.argumentsError is ZodError |
| 127 | + const argsErr: ZodError | undefined = (issue as any).argumentsError |
| 128 | + if (argsErr) { |
| 129 | + return argsErr.issues.flatMap((nestedIssue) => |
| 130 | + collectAllLeafIssues(nestedIssue), |
| 131 | + ) |
| 132 | + } |
| 133 | + return [issue] |
| 134 | + } |
| 135 | + |
| 136 | + case ZodIssueCode.invalid_return_type: { |
| 137 | + // invalid_return_type.issue.returnTypeError is ZodError |
| 138 | + const retErr: ZodError | undefined = (issue as any).returnTypeError |
| 139 | + if (retErr) { |
| 140 | + return retErr.issues.flatMap((nestedIssue) => |
| 141 | + collectAllLeafIssues(nestedIssue), |
| 142 | + ) |
| 143 | + } |
| 144 | + return [issue] |
| 145 | + } |
| 146 | + |
| 147 | + default: |
| 148 | + // Any other issue code is considered a “leaf” (no deeper nested ZodError) |
| 149 | + return [issue] |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +function getZodIssueMessage(issue: ZodIssue): string { |
| 154 | + if (issue.code === ZodIssueCode.invalid_type) { |
| 155 | + const attribute = issue.path[issue.path.length - 1] |
| 156 | + if (typeof attribute === 'string') { |
| 157 | + return `Expected type \`${issue.expected}\` for attribute "${attribute}", but received \`${issue.received}\`.` |
| 158 | + } |
| 159 | + |
| 160 | + return `Expected type \`${issue.expected}\`, but received \`${issue.received}\`.` |
| 161 | + } |
| 162 | + if (issue.code === ZodIssueCode.invalid_literal) { |
| 163 | + const attribute = issue.path[issue.path.length - 1] |
| 164 | + if (typeof attribute === 'string') { |
| 165 | + return `Expected literal \`${issue.expected}\` for attribute "${attribute}", but received \`${issue.received}\`.` |
| 166 | + } |
| 167 | + |
| 168 | + return `Expected literal \`${issue.expected}\`, but received \`${issue.received}\`.` |
| 169 | + } |
| 170 | + if (issue.code === ZodIssueCode.unrecognized_keys) { |
| 171 | + return `Unrecognized keys: ${issue.keys.join(', ')}.` |
| 172 | + } |
| 173 | + if (issue.code === ZodIssueCode.invalid_union) { |
| 174 | + return `Invalid union: ${issue.unionErrors |
| 175 | + .map((err) => err.message) |
| 176 | + .join(', ')}` |
| 177 | + } |
| 178 | + if (issue.code === ZodIssueCode.invalid_union_discriminator) { |
| 179 | + return `Invalid union discriminator. Expected one of: ${issue.options.join( |
| 180 | + ', ', |
| 181 | + )}.` |
| 182 | + } |
| 183 | + if (issue.code === ZodIssueCode.invalid_enum_value) { |
| 184 | + return `Invalid enum value: ${issue.received}. Expected one of: ${issue.options.join( |
| 185 | + ', ', |
| 186 | + )}.` |
| 187 | + } |
| 188 | + if (issue.code === ZodIssueCode.invalid_arguments) { |
| 189 | + return `Invalid arguments: ${issue.argumentsError.issues |
| 190 | + .map((err) => err.message) |
| 191 | + .join(', ')}` |
| 192 | + } |
| 193 | + if (issue.code === ZodIssueCode.invalid_return_type) { |
| 194 | + return `Invalid return type: ${issue.returnTypeError.issues |
| 195 | + .map((err) => err.message) |
| 196 | + .join(', ')}` |
| 197 | + } |
| 198 | + if (issue.code === ZodIssueCode.invalid_date) { |
| 199 | + return `Invalid date: ${issue.message || 'Invalid date format.'}` |
| 200 | + } |
| 201 | + if (issue.code === ZodIssueCode.invalid_string) { |
| 202 | + return `Invalid string: ${issue.message || 'String does not match expected format.'}` |
| 203 | + } |
| 204 | + if (issue.code === ZodIssueCode.too_small) { |
| 205 | + return `Value is too small: ${issue.message || 'Value does not meet minimum size.'}` |
| 206 | + } |
| 207 | + if (issue.code === ZodIssueCode.too_big) { |
| 208 | + return `Value is too big: ${issue.message || 'Value exceeds maximum size.'}` |
| 209 | + } |
| 210 | + if (issue.code === ZodIssueCode.invalid_intersection_types) { |
| 211 | + return `Invalid intersection types: ${issue.message || 'Types do not match.'}` |
| 212 | + } |
| 213 | + if (issue.code === ZodIssueCode.not_multiple_of) { |
| 214 | + return `Value is not a multiple of ${issue.multipleOf}: ${issue.message || 'Value does not meet multiple of condition.'}` |
| 215 | + } |
| 216 | + if (issue.code === ZodIssueCode.not_finite) { |
| 217 | + return `Value is not finite: ${issue.message || 'Value must be a finite number.'}` |
| 218 | + } |
| 219 | + if (issue.code === ZodIssueCode.custom) { |
| 220 | + return `Custom validation error: ${issue.message || 'No additional message provided.'}` |
| 221 | + } |
| 222 | + // For any other issue code, return the message directly |
| 223 | + return (issue as ZodIssue).message || 'Unknown validation error.' |
| 224 | +} |
| 225 | + |
| 226 | +export function getMostSpecificError(error: ZodIssue): { |
| 227 | + message: string |
| 228 | + path: (string | number)[] |
| 229 | +} { |
| 230 | + const allIssues = collectAllLeafIssues(error) |
| 231 | + |
| 232 | + if (allIssues.length === 0) { |
| 233 | + return { message: error.message, path: [] } |
| 234 | + } |
| 235 | + |
| 236 | + let mostSpecific = allIssues[0]! |
| 237 | + for (const issue of allIssues) { |
| 238 | + if (issue.path.length > mostSpecific.path.length) { |
| 239 | + mostSpecific = issue |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + return { |
| 244 | + message: getZodIssueMessage(mostSpecific), |
| 245 | + path: mostSpecific.path, |
| 246 | + } |
| 247 | +} |
0 commit comments