medusajs/medusa · error · MedusaError

Countries with codes: "${getDuplicates(countries).join(", ")

Error message

Countries with codes: "${getDuplicates(countries).join(", ")}" are already assigned to a region

What it means

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.

Source

Thrown at packages/modules/region/src/services/region-module.ts:337

   * Validate that countries can be assigned to a region.
   *
   * NOTE: this method relies on countries of the regions that we are assigning to need to be unassigned first.
   * @param countries
   * @param sharedContext
   * @private
   */
  private async validateCountries(
    countries: string[] | undefined,
    sharedContext: Context
  ): Promise<InferEntityType<typeof Country>[]> {
    if (!countries?.length) {
      return []
    }

    // The new regions being created have a country conflict
    const uniqueCountries = Array.from(new Set(countries))
    if (uniqueCountries.length !== countries.length) {
      throw new MedusaError(
        MedusaError.Types.INVALID_DATA,
        `Countries with codes: "${getDuplicates(countries).join(
          ", "
        )}" are already assigned to a region`
      )
    }

    const countriesInDb = await this.countryService_.list(
      { iso_2: uniqueCountries },
      { select: ["iso_2", "region_id"] },
      sharedContext
    )
    const countryCodesInDb = countriesInDb.map((c) => c.iso_2.toLowerCase())

    // Countries missing in the database
    if (countriesInDb.length !== uniqueCountries.length) {
      const missingCountries = arrayDifference(
        uniqueCountries,

View on GitHub (pinned to 5e06e544a2)

Solutions

  1. Deduplicate the countries array before sending: Array.from(new Set(countries))
  2. Fix the upstream data source that produced duplicates (seed file, CSV import, form state)
  3. Add payload validation in a workflow step so duplicates are caught with a clearer error

Example fix

// before
await regionService.createRegions([{ name: "NA", countries: ["us", "us", "ca"] }])
// after
await regionService.createRegions([{ name: "NA", countries: [...new Set(["us", "ca"])] }])
Defensive patterns

Strategy: validation

Validate before calling

function dedupeCountries(payload: { countries?: string[] }[]) {
  for (const p of payload) {
    const unique = [...new Set(p.countries ?? [])]
    if (unique.length !== (p.countries ?? []).length) {
      p.countries = unique
    }
  }
}

Type guard

function hasNoDuplicateCountries(countries: string[]): boolean {
  return new Set(countries).size === countries.length
}

Prevention

When it happens

Trigger: Calling createRegions or updateRegions with a countries array containing the same iso_2 code twice, e.g. { countries: ['us','us','ca'] }.

Common situations: Merging country lists from multiple sources without deduping, seed scripts repeating entries, or UI multi-selects that allow duplicate submissions.

Related errors


AI-assisted analysis of medusajs/medusa@5e06e544a2 (2026-08-27). Data as JSON: /api/errors/1842915b75574af1. Report an issue: GitHub.