{"record":{"id":"1842915b75574af1","repo":"medusajs/medusa","slug":"countries-with-codes-getduplicates-countries","errorCode":null,"errorMessage":"Countries with codes: \"${getDuplicates(countries).join(\", \")}\" are already assigned to a region","messagePattern":"Countries with codes: \"(.+?)\" are already assigned to a region","errorType":"validation","errorClass":"MedusaError","httpStatus":400,"severity":"error","filePath":"packages/modules/region/src/services/region-module.ts","lineNumber":337,"sourceCode":"   * Validate that countries can be assigned to a region.\n   *\n   * NOTE: this method relies on countries of the regions that we are assigning to need to be unassigned first.\n   * @param countries\n   * @param sharedContext\n   * @private\n   */\n  private async validateCountries(\n    countries: string[] | undefined,\n    sharedContext: Context\n  ): Promise<InferEntityType<typeof Country>[]> {\n    if (!countries?.length) {\n      return []\n    }\n\n    // The new regions being created have a country conflict\n    const uniqueCountries = Array.from(new Set(countries))\n    if (uniqueCountries.length !== countries.length) {\n      throw new MedusaError(\n        MedusaError.Types.INVALID_DATA,\n        `Countries with codes: \"${getDuplicates(countries).join(\n          \", \"\n        )}\" are already assigned to a region`\n      )\n    }\n\n    const countriesInDb = await this.countryService_.list(\n      { iso_2: uniqueCountries },\n      { select: [\"iso_2\", \"region_id\"] },\n      sharedContext\n    )\n    const countryCodesInDb = countriesInDb.map((c) => c.iso_2.toLowerCase())\n\n    // Countries missing in the database\n    if (countriesInDb.length !== uniqueCountries.length) {\n      const missingCountries = arrayDifference(\n        uniqueCountries,","sourceCodeStart":319,"sourceCodeEnd":355,"githubUrl":"https://github.com/medusajs/medusa/blob/5e06e544a296b9033f20f71f11c559f81a0e5739/packages/modules/region/src/services/region-module.ts#L319-L355","documentation":"Thrown by the region module's validateCountries when the input list for a single region contains duplicate country codes. The module de-duplicates to detect internal duplicates (within the same payload) and rejects with INVALID_DATA before any DB write.","triggerScenarios":"Calling createRegions or updateRegions with a countries array containing the same iso_2 code twice, e.g. { countries: ['us','us','ca'] }.","commonSituations":"Merging country lists from multiple sources without deduping, seed scripts repeating entries, or UI multi-selects that allow duplicate submissions.","solutions":["Deduplicate the countries array before sending: Array.from(new Set(countries))","Fix the upstream data source that produced duplicates (seed file, CSV import, form state)","Add payload validation in a workflow step so duplicates are caught with a clearer error"],"exampleFix":"// before\nawait regionService.createRegions([{ name: \"NA\", countries: [\"us\", \"us\", \"ca\"] }])\n// after\nawait regionService.createRegions([{ name: \"NA\", countries: [...new Set([\"us\", \"ca\"])] }])","handlingStrategy":"validation","validationCode":"function dedupeCountries(payload: { countries?: string[] }[]) {\n  for (const p of payload) {\n    const unique = [...new Set(p.countries ?? [])]\n    if (unique.length !== (p.countries ?? []).length) {\n      p.countries = unique\n    }\n  }\n}","typeGuard":"function hasNoDuplicateCountries(countries: string[]): boolean {\n  return new Set(countries).size === countries.length\n}","tryCatchPattern":null,"preventionTips":["Always dedupe country arrays before create/update region calls","Dedupe merged lists from multiple sources (imports, multi-select forms)","Validate payloads in a workflow step with zod .transform deduping"],"tags":["region","countries","duplicate-data","validation"],"backgroundTag":"duplicate-entries-rejected","analyzedSha":"5e06e544a296b9033f20f71f11c559f81a0e5739","analyzedAt":"2026-08-27T07:24:39.599Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}