{"version":3,"file":"zod.mjs","sources":["../../../server/src/validation/zod.ts"],"sourcesContent":["import { z, errors } from '@strapi/utils';\n\ninterface FormattedZodError {\n  path: string[];\n  message: string;\n  name: 'ValidationError';\n  value: undefined;\n}\n\ninterface FormattedZodErrors {\n  errors: FormattedZodError[];\n  message: string;\n}\n\n/**\n * Transforms a ZodError into the same shape as formatYupErrors from @strapi/utils.\n * Only keeps the first error per path to match Yup behavior.\n */\nconst formatZodErrors = (zodError: z.ZodError): FormattedZodErrors => {\n  const seen = new Set<string>();\n  const formattedErrors: FormattedZodError[] = [];\n\n  for (const issue of zodError.issues) {\n    const key = issue.path.join('.');\n    if (!seen.has(key)) {\n      seen.add(key);\n      formattedErrors.push({\n        path: issue.path.map(String),\n        message: issue.message,\n        name: 'ValidationError',\n        value: undefined,\n      });\n    }\n  }\n\n  return {\n    errors: formattedErrors,\n    message: zodError.issues[0]?.message ?? 'Validation error',\n  };\n};\n\n/**\n * Zod schema for Strapi entity IDs.\n * Matches the StrapiIDSchema from @strapi/utils/yup:\n * accepts strings or non-negative integers.\n */\nconst strapiID = z.union([z.string(), z.number().int().nonnegative()]);\n\n/**\n * Async Zod validator matching the signature of validateYupSchema from @strapi/utils.\n *\n * Usage:\n *   const validate = validateZodAsync(mySchema);\n *   const data = await validate(body);            // throws ValidationError on failure\n *   const data = await validate(body, 'Custom');  // throws with custom message\n */\nconst validateZodAsync =\n  <T extends z.Schema>(schema: T) =>\n  async (data: unknown, errorMessage?: string): Promise<z.infer<T>> => {\n    try {\n      return await schema.parseAsync(data);\n    } catch (error) {\n      if (error instanceof z.ZodError) {\n        const { message, errors: formattedErrors } = formatZodErrors(error);\n        throw new errors.ValidationError(errorMessage || message, {\n          errors: formattedErrors,\n        });\n      }\n      throw error;\n    }\n  };\n\nexport { formatZodErrors, strapiID, validateZodAsync };\nexport type { FormattedZodError, FormattedZodErrors };\n"],"names":["formatZodErrors","zodError","seen","Set","formattedErrors","issue","issues","key","path","join","has","add","push","map","String","message","name","value","undefined","errors","strapiID","z","union","string","number","int","nonnegative","validateZodAsync","schema","data","errorMessage","parseAsync","error","ZodError","ValidationError"],"mappings":";;AAcA;;;IAIA,MAAMA,kBAAkB,CAACC,QAAAA,GAAAA;AACvB,IAAA,MAAMC,OAAO,IAAIC,GAAAA,EAAAA;AACjB,IAAA,MAAMC,kBAAuC,EAAE;AAE/C,IAAA,KAAK,MAAMC,KAAAA,IAASJ,QAAAA,CAASK,MAAM,CAAE;AACnC,QAAA,MAAMC,GAAAA,GAAMF,KAAAA,CAAMG,IAAI,CAACC,IAAI,CAAC,GAAA,CAAA;AAC5B,QAAA,IAAI,CAACP,IAAAA,CAAKQ,GAAG,CAACH,GAAAA,CAAAA,EAAM;AAClBL,YAAAA,IAAAA,CAAKS,GAAG,CAACJ,GAAAA,CAAAA;AACTH,YAAAA,eAAAA,CAAgBQ,IAAI,CAAC;AACnBJ,gBAAAA,IAAAA,EAAMH,KAAAA,CAAMG,IAAI,CAACK,GAAG,CAACC,MAAAA,CAAAA;AACrBC,gBAAAA,OAAAA,EAASV,MAAMU,OAAO;gBACtBC,IAAAA,EAAM,iBAAA;gBACNC,KAAAA,EAAOC;AACT,aAAA,CAAA;AACF,QAAA;AACF,IAAA;IAEA,OAAO;QACLC,MAAAA,EAAQf,eAAAA;AACRW,QAAAA,OAAAA,EAASd,QAAAA,CAASK,MAAM,CAAC,CAAA,CAAE,EAAES,OAAAA,IAAW;AAC1C,KAAA;AACF;AAEA;;;;AAIC,IACD,MAAMK,QAAAA,GAAWC,CAAAA,CAAEC,KAAK,CAAC;AAACD,IAAAA,CAAAA,CAAEE,MAAM,EAAA;AAAIF,IAAAA,CAAAA,CAAEG,MAAM,EAAA,CAAGC,GAAG,EAAA,CAAGC,WAAW;AAAG,CAAA;AAErE;;;;;;;AAOC,IACD,MAAMC,gBAAAA,GACJ,CAAqBC,MAAAA,GACrB,OAAOC,IAAAA,EAAeC,YAAAA,GAAAA;QACpB,IAAI;YACF,OAAO,MAAMF,MAA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