Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/validators/iban.py: 100%
10 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:08 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:08 +0000
1"""IBAN."""
2# -*- coding: utf-8 -*-
4# standard
5import re
7# local
8from .utils import validator
11def _char_value(char: str):
12 """A=10, B=11, ..., Z=35."""
13 return char if char.isdigit() else str(10 + ord(char) - ord("A"))
16def _mod_check(value: str):
17 """Check if the value string passes the mod97-test."""
18 # move country code and check numbers to end
19 rearranged = value[4:] + value[:4]
20 return int("".join(_char_value(char) for char in rearranged)) % 97 == 1
23@validator
24def iban(value: str, /):
25 """Return whether or not given value is a valid IBAN code.
27 Examples:
28 >>> iban('DE29100500001061045672')
29 # Output: True
30 >>> iban('123456')
31 # Output: ValidationFailure(func=iban, ...)
33 Args:
34 value:
35 IBAN string to validate.
37 Returns:
38 (Literal[True]):
39 If `value` is a valid IBAN code.
40 (ValidationFailure):
41 If `value` is an invalid IBAN code.
43 > *New in version 0.8.0*
44 """
45 return (
46 (re.match(r"^[A-Z]{2}[0-9]{2}[A-Z0-9]{11,30}$", value) and _mod_check(value))
47 if value
48 else False
49 )