dify/web/app/components/base/form/utils/zod-submit-validator.ts
Stephen Zhou a84c2d36a3
style: format with vp fmt (#38803)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-07-12 15:57:46 +00:00

25 lines
680 B
TypeScript

import type { ZodSchema } from 'zod'
type SubmitValidator<T> = ({
value,
}: {
value: T
}) => { fields: Record<string, string> } | undefined
export const zodSubmitValidator = <T>(schema: ZodSchema<T>): SubmitValidator<T> => {
return ({ value }) => {
const result = schema.safeParse(value)
if (!result.success) {
const fieldErrors: Record<string, string> = {}
for (const issue of result.error.issues) {
const path = issue.path[0]
if (path === undefined) continue
const key = String(path)
if (!fieldErrors[key]) fieldErrors[key] = issue.message
}
return { fields: fieldErrors }
}
return undefined
}
}