( schema: T, input: unknown, )
| 300 | * Safe parse wrapper that returns a formatted error message |
| 301 | */ |
| 302 | export function safeParse<T extends v.GenericSchema>( |
| 303 | schema: T, |
| 304 | input: unknown, |
| 305 | ): { success: true; data: v.InferOutput<T> } | { success: false; error: string } { |
| 306 | const result = v.safeParse(schema, input) |
| 307 | if (result.success) { |
| 308 | return { success: true, data: result.output } |
| 309 | } |
| 310 | // Format the first error message |
| 311 | const firstIssue = result.issues[0] |
| 312 | const path = firstIssue?.path?.map(p => p.key).join('.') || '' |
| 313 | const message = firstIssue?.message || 'Validation failed' |
| 314 | return { |
| 315 | success: false, |
| 316 | error: path ? `${path}: ${message}` : message, |
| 317 | } |
| 318 | } |
no outgoing calls
no test coverage detected