{"version":3,"sources":["../../src/errors/ValidationError.ts"],"sourcesContent":["import type { TFunction } from '@payloadcms/translations'\n\nimport { en } from '@payloadcms/translations/languages/en'\nimport { status as httpStatus } from 'http-status'\n\nimport type { LabelFunction, StaticLabel } from '../config/types.js'\nimport type { PayloadRequest } from '../types/index.js'\n\nimport { APIError } from './APIError.js'\n\n/** @deprecated Use `instanceof ValidationError` instead of name comparison. */\nexport const ValidationErrorName = 'ValidationError'\n\nexport type ValidationFieldError = {\n  label?: LabelFunction | StaticLabel\n  // The error message to display for this field\n  message: string\n  path: string\n}\n\nexport class ValidationError extends APIError<{\n  collection?: string\n  errors: ValidationFieldError[]\n  global?: string\n}> {\n  constructor(\n    results: {\n      collection?: string\n      errors: ValidationFieldError[]\n      global?: string\n      id?: number | string\n      /**\n       *  req needs to be passed through (if you have one) in order to resolve label functions that may be part of the errors array\n       */\n      req?: Partial<PayloadRequest>\n    },\n    t?: TFunction,\n  ) {\n    const message = t\n      ? t('error:followingFieldsInvalid', { count: results.errors.length })\n      : results.errors.length === 1\n        ? en.translations.error.followingFieldsInvalid_one\n        : en.translations.error.followingFieldsInvalid_other\n\n    const req = results.req\n    // delete to avoid logging the whole req\n    delete results['req']\n\n    super(\n      `${message} ${results.errors\n        .map((f) => {\n          if (f.label) {\n            if (typeof f.label === 'function') {\n              if (!req || !req.i18n || !req.t) {\n                return f.path\n              }\n\n              return f.label({ i18n: req.i18n, t: req.t })\n            }\n\n            if (typeof f.label === 'object') {\n              if (req?.i18n?.language) {\n                return f.label[req.i18n.language]\n              }\n\n              return f.label[Object.keys(f.label)[0]!]\n            }\n\n            return f.label\n          }\n\n          return f.path\n        })\n        .join(', ')}`,\n      httpStatus.BAD_REQUEST,\n      results,\n    )\n  }\n}\n"],"names":["en","status","httpStatus","APIError","ValidationErrorName","ValidationError","results","t","message","count","errors","length","translations","error","followingFieldsInvalid_one","followingFieldsInvalid_other","req","map","f","label","i18n","path","language","Object","keys","join","BAD_REQUEST"],"mappings":"AAEA,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