{"record":{"id":"3c90e1b25b08d409","repo":"medusajs/medusa","slug":"countries-with-codes-missingcountries-join","errorCode":null,"errorMessage":"Countries with codes: \"${missingCountries.join(\", \")}\" do not exist","messagePattern":"Countries with codes: \"(.+?)\" do not exist","errorType":"validation","errorClass":"MedusaError","httpStatus":400,"severity":"error","filePath":"packages/modules/region/src/services/region-module.ts","lineNumber":359,"sourceCode":"        )}\" 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,\n        countryCodesInDb\n      )\n\n      throw new MedusaError(\n        MedusaError.Types.INVALID_DATA,\n        `Countries with codes: \"${missingCountries.join(\", \")}\" do not exist`\n      )\n    }\n\n    // Countries that already have a region already assigned to them\n    // @ts-ignore\n    const countriesWithRegion = countriesInDb.filter((c) => !!c.region_id)\n    if (countriesWithRegion.length) {\n      throw new MedusaError(\n        MedusaError.Types.INVALID_DATA,\n        `Countries with codes: \"${countriesWithRegion\n          .map((c) => c.iso_2)\n          .join(\", \")}\" are already assigned to a region`\n      )\n    }\n\n    return countriesInDb","sourceCodeStart":341,"sourceCodeEnd":377,"githubUrl":"https://github.com/medusajs/medusa/blob/5e06e544a296b9033f20f71f11c559f81a0e5739/packages/modules/region/src/services/region-module.ts#L341-L377","documentation":"Thrown by the region module's validateCountries when one or more requested country codes do not exist in the installed country data. The module compares the input codes against countries in the DB (arrayDifference) and reports the exact missing codes with INVALID_DATA.","triggerScenarios":"Calling createRegions/updateRegions with invalid or unsupported ISO 2 codes, e.g. 'zz', 'usa' (3-letter), or lowercase/non-canonical casing not normalized upstream.","commonSituations":"Passing ISO 3166-1 alpha-3 codes instead of alpha-2, typos in seed files, codes for countries not present in the loaded country data, or casing issues ('US' vs 'us') depending on normalization.","solutions":["Check the missing codes in the message and correct them to valid ISO 3166-1 alpha-2 codes","Ensure codes are lowercase ('us', 'dk') as expected by the region module's normalization","If a legitimately supported country is missing, verify your Medusa version/country data is up to date"],"exampleFix":"// before\nawait regionService.createRegions([{ name: \"NA\", countries: [\"usa\", \"can\"] }])\n// after\nawait regionService.createRegions([{ name: \"NA\", countries: [\"us\", \"ca\"] }])","handlingStrategy":"validation","validationCode":"const VALID_ISO2 = /^[a-z]{2}$/ // region module uses lowercase alpha-2\nfunction validateCountryCodes(codes: string[]) {\n  const invalid = codes.filter((c) => !VALID_ISO2.test(c))\n  if (invalid.length) throw new Error(`Invalid country codes: ${invalid.join(\", \")}`)\n}","typeGuard":"function isValidIso2(code: string): boolean {\n  return /^[a-z]{2}$/.test(code)\n}","tryCatchPattern":"try {\n  await regionModule.createRegions(payload)\n} catch (e) {\n  if (e instanceof MedusaError && /do not exist/.test(e.message)) {\n    // strip the listed codes and retry with only valid ones\n  } else throw e\n}","preventionTips":["Use ISO 3166-1 alpha-2 codes in lowercase","Validate codes against a country list constant at the edge (API/form)","Keep country data up to date with your Medusa version"],"tags":["region","countries","invalid-code","validation"],"backgroundTag":"invalid-country-code","analyzedSha":"5e06e544a296b9033f20f71f11c559f81a0e5739","analyzedAt":"2026-08-27T07:24:39.599Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}