18 lines
510 B
TypeScript
18 lines
510 B
TypeScript
import type { ZodError } from 'zod'
|
|
|
|
export type FieldErrors<T extends Record<string, unknown>> = Partial<Record<keyof T, string>>
|
|
|
|
export const mapZodErrors = <T extends Record<string, unknown>>(error: ZodError<T>): FieldErrors<T> => {
|
|
const flattened = error.flatten().fieldErrors
|
|
const result: FieldErrors<T> = {}
|
|
|
|
for (const key in flattened) {
|
|
const message = flattened[key]?.[0]
|
|
if (message) {
|
|
result[key as keyof T] = message
|
|
}
|
|
}
|
|
|
|
return result
|
|
}
|