{"record":{"id":"c15fb7066c321790","repo":"medusajs/medusa","slug":"country-with-code-shippingaddress-country-code","errorCode":null,"errorMessage":"Country with code ${shippingAddress.country_code} is not within region ${data.region.name}","messagePattern":"Country with code (.+?) is not within region (.+?)","errorType":"exception","errorClass":"MedusaError","httpStatus":400,"severity":"error","filePath":"packages/core/core-flows/src/cart/workflows/update-cart.ts","lineNumber":78,"sourceCode":"    const data_ = {\n      ...updateCartData,\n      currency_code: data.region?.currency_code,\n      region_id: data.region?.id, // This is either the region from the input or the region from the cart or null\n    }\n\n    // When the region is updated, we do a few things:\n    // - We need to make sure the provided shipping address country code is in the new region\n    // - We clear the shipping address if the new region has more than one country\n    const regionIsNew = data.region?.id !== data.cartToUpdate.region?.id\n    const shippingAddress = data.input.shipping_address\n\n    if (shippingAddress?.country_code) {\n      const country = data.region.countries.find(\n        (c: { iso_2: string }) => c.iso_2 === shippingAddress.country_code\n      )\n\n      if (!country) {\n        throw new MedusaError(\n          MedusaError.Types.INVALID_DATA,\n          `Country with code ${shippingAddress.country_code} is not within region ${data.region.name}`\n        )\n      }\n\n      data_.shipping_address = {\n        ...shippingAddress,\n        country_code: country.iso_2,\n      }\n    }\n\n    if (regionIsNew) {\n      if (data.region.countries.length === 1) {\n        data_.shipping_address = {\n          country_code: data.region.countries[0].iso_2,\n        }\n      }\n","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/medusajs/medusa/blob/5e06e544a296b9033f20f71f11c559f81a0e5739/packages/core/core-flows/src/cart/workflows/update-cart.ts#L60-L96","documentation":"updateCartWorkflow validates that, when a shipping address country_code is provided, that country belongs to the cart's region (regions enumerate allowed countries). A mismatch throws invalid_data because taxes and shipping depend on region-country consistency.","triggerScenarios":"Calling updateCart with shipping_address.country_code set to a country that is not in the cart region's countries list — e.g. address in 'us' while the region only includes EU countries.","commonSituations":"Default region covering few countries while customers enter addresses from anywhere; address forms not filtered by region; changing the address without switching the cart's region; regions misconfigured after launch excluding expected countries.","solutions":["Add the country to the region's countries via Admin (POST /admin/regions/:id/countries) and retry","Or set the cart's region_id to a region that includes the customer's country before updating the address","Restrict the address form's country selector to the cart region's countries"],"exampleFix":"// before\nawait updateCartWorkflow(container).run({ input: { id: cart_id, shipping_address: { country_code: 'ca', ... } } })\n\n// after (only allow region countries in the form)\nconst { data: [region] } = await query.graph({ entity: 'region', fields: ['countries.iso_2'], filters: { id: cart.region_id } })\nconst allowed = region.countries.map((c) => c.iso_2)\nif (!allowed.includes(address.country_code)) throw new Error('Pick a supported country')\nawait updateCartWorkflow(container).run({ input: { id: cart_id, shipping_address: address } })","handlingStrategy":"validation","validationCode":"const { data: [region] } = await query.graph({ entity: 'region', fields: ['countries.iso_2'], filters: { id: cart.region_id } })\nconst allowed = region.countries.map((c) => c.iso_2)\nif (!allowed.includes(address.country_code)) throw new Error('Country not in cart region — switch region or pick allowed country')","typeGuard":"const isCountryInRegion = (code: string, region: { countries: { iso_2: string }[] }) =>\n  region.countries.some((c) => c.iso_2 === code)","tryCatchPattern":null,"preventionTips":["Populate the address form's country dropdown from the cart region's countries","When customer changes country, update the cart's region in the same action","Keep region countries in sync with your shipping coverage"],"tags":["cart","region","address","country","validation"],"backgroundTag":"country-not-in-region","analyzedSha":"5e06e544a296b9033f20f71f11c559f81a0e5739","analyzedAt":"2026-08-27T07:24:39.599Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}