{"version":3,"file":"validator.cjs","sources":["../../src/validator.ts"],"sourcesContent":["import type {\n  ValidationError,\n  Validator,\n  ValidatorAdapterParams,\n} from '@tanstack/form-core'\nimport type { ZodIssue, ZodType } from 'zod'\n\ntype Params = ValidatorAdapterParams<ZodIssue>\ntype TransformFn = NonNullable<Params['transformErrors']>\n\nexport function prefixSchemaToErrors(\n  zodErrors: ZodIssue[],\n  transformErrors: TransformFn,\n) {\n  const schema = new Map<string, ZodIssue[]>()\n\n  for (const zodError of zodErrors) {\n    const path = zodError.path\n      .map((segment) =>\n        typeof segment === 'number' ? `[${segment}]` : segment,\n      )\n      .join('.')\n      .replace(/\\.\\[/g, '[')\n    schema.set(path, (schema.get(path) ?? []).concat(zodError))\n  }\n\n  const transformedSchema = {} as Record<string, ValidationError>\n\n  schema.forEach((value, key) => {\n    transformedSchema[key] = transformErrors(value)\n  })\n\n  return transformedSchema\n}\n\nexport function defaultFormTransformer(transformErrors: TransformFn) {\n  return (zodErrors: ZodIssue[]) => ({\n    form: transformErrors(zodErrors),\n    fields: prefixSchemaToErrors(zodErrors, transformErrors),\n  })\n}\n\n/**\n * @deprecated With zod 3.24.0 the adapter is no longer needed and will be soon removed.\n * If you were passing some parameters you can use the `standardSchemaValidator` instead.\n */\nexport const zodValidator =\n  (params: Params = {}): Validator<unknown, ZodType> =>\n  () => {\n    const transformFieldErrors =\n      params.transformErrors ??\n      ((issues: ZodIssue[]) => issues.map((issue) => issue.message).join(', '))\n\n    const getTransformStrategy = (validationSource: 'form' | 'field') =>\n      validationSource === 'form'\n        ? defaultFormTransformer(transformFieldErrors)\n        : transformFieldErrors\n\n    return {\n      validate({ value, validationSource }, fn) {\n        const result = fn.safeParse(value)\n        if (result.success) return\n\n        const transformer = getTransformStrategy(validationSource)\n\n        return transformer(result.error.issues)\n      },\n      async validateAsync({ value, validationSource }, fn) {\n        const result = await fn.safeParseAsync(value)\n        if (result.success) return\n\n        const transformer = getTransformStrategy(validationSource)\n\n        return transformer(result.error.issues)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