{"version":3,"file":"zod.mjs","sources":["../src/zod.ts"],"sourcesContent":["import * as z from 'zod/v4';\n\nimport { ValidationError } from './errors';\n\n/**\n * Re-export of the Zod v4 schema builder from the same version Strapi uses\n * internally. Use this for building schemas passed to content API param\n * registration (addQueryParams / addInputParams) so your code stays compatible\n * across Strapi minor/patch updates.\n *\n * @example\n * import { z } from '@strapi/utils';\n * strapi.contentAPI.addQueryParams({\n *   search: {\n *     schema: z.string().max(200).optional(),\n *     matchRoute: (route) => route.path.includes('articles'),\n *   },\n * });\n */\nexport { z };\n\nexport const validateZodSchema =\n  <T extends z.Schema>(schema: T) =>\n  (data: unknown, errorMessage?: string): z.infer<T> => {\n    try {\n      return schema.parse(data);\n    } catch (error) {\n      if (error instanceof z.ZodError) {\n        const { message, errors } = formatZodErrors(error);\n        throw new ValidationError(errorMessage || message, { errors });\n      }\n\n      throw error;\n    }\n  };\n\nconst formatZodErrors = (zodError: z.ZodError) => ({\n  errors: zodError.issues.map((issue) => {\n    return {\n      path: issue.path.map(String),\n      message: issue.message,\n      name: 'ValidationError',\n      value: undefined,\n    };\n  }),\n  message: zodError.issues[0]?.message ?? 'Validation error',\n});\n\ntype FormErrors = Record<string, string>;\n\n/**\n * Converts a ZodError into form-compatible errors matching\n * getYupValidationErrors from @strapi/admin Form component.\n *\n * Returns a flat object with dot-path keys and string error messages.\n * Only the first error per path is kept (matches Yup behavior).\n * Root-level errors (path = []) are stored under the empty string key ''.\n */\nexport const getZodValidationErrors = (error: z.ZodError): FormErrors => {\n  const errors: FormErrors = {};\n\n  for (const issue of error.issues) {\n    const path = issue.path.join('.');\n    if (!(path in errors)) {\n      errors[path] = issue.message;\n    }\n  }\n\n  return errors;\n};\n"],"names":["validateZodSchema","schema","data","errorMessage","parse","error","z","ZodError","message","errors","formatZodErrors","ValidationError","zodError","issues","map","issue","path","String","name","value","undefined","getZodValidationErrors","join"],"mappings":";;;;AAqBO,MAAMA,iBAAAA,GACX,CAAqBC,MAAAA,GACrB,CAACC,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