validateCountryCode function

dynamic validateCountryCode(
  1. String iban, [
  2. bool hasStructure = true
])

Implementation

validateCountryCode(String iban, [bool hasStructure = true]) {
  // check if iban contains 2 char country code
  if (iban.length < COUNTRY_CODE_LENGTH) {
    throw Exception("Iban must contain 2 char country code.");
  }

  final String countryCode = getCountryCode(iban);

  // check case sensitivity
  if (countryCode != countryCode.toUpperCase() ||
      !RegExp(ucRegex).hasMatch(countryCode)) {
    throw Exception("Iban country code must contain upper case letters.");
  }

  final Country? country = Country.countryByCode(countryCode);
  if (country == null) {
    throw Exception("Iban contains non existing country code.");
  }

  if (hasStructure) {
    // check if country is supported
    final BbanStructure? structure = BbanStructure.forCountry(country);
    if (structure == null) {
      throw Exception("Country code is not supported.");
    }
  }
}