validate function
validate an iban
Throws ValueException
if an invalid ibanString
is provided
InvalidBbanException
if Basic Bank Account Number is invalid
InvalidCheckDigitException
if the check digit is invalid
UnexpectedConstantValue
if a constant value was not found
InvalidMod97Exception
if mod 97 validation result is invalid
Implementation
Future<void> validate(String iban, IbanSpec spec) async {
var spec = await _getIbanSpec(iban.substring(0, 2));
if (spec == null) {
throw ValueException("This country doesn't support iban");
}
if (spec.length != iban.length) {
throw ValueException('Invalid Iban length');
}
if (_hasMatch(spec.patterns.bban, iban) == false) {
var field = iban.substring(4);
throw InvalidBbanException('Invalid Basic Bank Account Number', field);
}
if (_hasMatch(spec.patterns.checkDigit, iban) == false) {
var field = iban.substring(2, 2);
throw InvalidCheckDigitException('Invalid Check Digit', field);
}
for (var constant in spec.patterns.constants) {
if (_hasMatch(constant.pattern, iban) == false) {
throw UnexpectedConstantValue(
'Invalid constant value', constant.constant, constant.position);
}
}
if (_calculateMod97(iban) != 1) {
throw InvalidMod97Exception('Invalid MOD 97 Operation Result');
}
}