{"record":{"id":"06bb2eb16bc504bc","repo":"bchavez/Bogus","slug":"the-country-code-must-be-an-iso3166-two-letter-cou","errorCode":null,"errorMessage":"The country code must be an ISO3166 two-letter country code.","messagePattern":"The country code must be an ISO3166 two-letter country code\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Source/Bogus/DataSets/Finance.cs","lineNumber":389,"sourceCode":"   /// </summary>\n   /// <param name=\"formatted\">Formatted IBAN containing spaces.</param>\n   /// <param name=\"countryCode\">A two letter ISO3166 country code. Throws an exception if the country code is not found or is an invalid length.</param>\n   /// <exception cref=\"KeyNotFoundException\">An exception is thrown if the ISO3166 country code is not found.</exception>\n   public string Iban(bool formatted = false, string countryCode = null)\n   {\n      var arr = this.GetArray(\"iban_formats\");\n\n      IBanFormat ibanFormat;\n      if( countryCode is null )\n      {\n         var formatEntry = (BObject)this.Random.ArrayElement(arr);\n         ibanFormat = this.GetIbanFormat(formatEntry);\n      }\n      else\n      {\n         if( countryCode.Length != 2 )\n         {\n            throw new ArgumentOutOfRangeException(nameof(countryCode), countryCode.Length, \"The country code must be an ISO3166 two-letter country code.\");\n         }\n\n         var formatEntry = arr.OfType<BObject>()\n            .Where(b => countryCode.Equals(b[\"country\"].StringValue, StringComparison.OrdinalIgnoreCase))\n            .FirstOrDefault();\n\n         if (formatEntry is null)\n         {\n            throw new KeyNotFoundException($\"The ISO3166 IBAN country code '{countryCode}' was not found.\");\n         }\n\n         ibanFormat = this.GetIbanFormat(formatEntry);\n      }\n\n      return Iban(ibanFormat, formatted);\n   }\n\n   protected string Iban(IBanFormat ibanFormat, bool formatted)","sourceCodeStart":371,"sourceCodeEnd":407,"githubUrl":"https://github.com/bchavez/Bogus/blob/6ece18c5c22648ab4606e45a0aaf2f07fc5259e4/Source/Bogus/DataSets/Finance.cs#L371-L407","documentation":"Finance.Iban(countryCode) demands an ISO 3166 two-letter (alpha-2) country code. If countryCode is non-null and its length is not exactly 2 it throws ArgumentOutOfRangeException reporting the actual length. Pass null to let Bogus pick a random IBAN country.","triggerScenarios":"Calling finance.Iban(countryCode: \"GBR\") (alpha-3), finance.Iban(\"UNITED_KINGDOM\"), finance.Iban(\"G\"), or finance.Iban(\"\") with a non-null string whose length is not 2.","commonSituations":"Passing an ISO alpha-3 code (3 letters) or a full country name where an alpha-2 code is required. Copying a code with whitespace or from uppercase mismatched sources.","solutions":["Pass a 2-letter ISO 3166 alpha-2 code, e.g. finance.Iban(\"GB\").","Pass null to have a random IBAN country chosen for you.","Normalize/trim the input string and assert its length is 2 before calling."],"exampleFix":"// before\nvar iban = finance.Iban(countryCode: \"GBR\");\n\n// after\nvar iban = finance.Iban(countryCode: \"GB\");","handlingStrategy":"validation","validationCode":"string countryCode = \"GB\";\nif (countryCode is not null && countryCode.Length != 2)\n    throw new ArgumentException(\"countryCode must be a 2-letter ISO 3166 alpha-2 code or null.\");\nvar iban = finance.Iban(countryCode);","typeGuard":"static bool IsValidAlpha2(string code) => code is null || (code.Length == 2 && code.All(char.IsLetter));","tryCatchPattern":null,"preventionTips":["Pass null when you do not need a specific IBAN country.","Keep a curated list of alpha-2 codes rather than free-text input.","Trim and ToUpperInvariant() user input before validation."],"tags":["finance","iban","argument-validation","iso3166"],"backgroundTag":null,"analyzedSha":"6ece18c5c22648ab4606e45a0aaf2f07fc5259e4","analyzedAt":"2026-08-13T21:08:15.463Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}