From a646425aaf8e4441cce516c1da313e647e911640 Mon Sep 17 00:00:00 2001 From: Mael Arnaud Date: Fri, 19 Jul 2024 10:28:50 +0200 Subject: [PATCH] feat: add whitespace filtering to iban validator - add a new function to filter whitespace to IBAN validator --- src/validators/iban.py | 13 +++++++++++-- tests/test_iban.py | 10 +++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/validators/iban.py b/src/validators/iban.py index 2b1a4d4d..726aa7a4 100644 --- a/src/validators/iban.py +++ b/src/validators/iban.py @@ -12,6 +12,11 @@ def _char_value(char: str): return char if char.isdigit() else str(10 + ord(char) - ord("A")) +def _filter_whitespace(value: str): + """Remove all kind of unicode whitespace from the string.""" + return "".join(filter(lambda x: not x.isspace(), value)) + + def _mod_check(value: str): """Check if the value string passes the mod97-test.""" # move country code and check numbers to end @@ -37,8 +42,12 @@ def iban(value: str, /): (Literal[True]): If `value` is a valid IBAN code. (ValidationError): If `value` is an invalid IBAN code. """ + filtered_value = _filter_whitespace(value) return ( - (re.match(r"^[a-z]{2}[0-9]{2}[a-z0-9]{11,30}$", value, re.IGNORECASE) and _mod_check(value)) - if value + ( + re.match(r"^[a-z]{2}[0-9]{2}[a-z0-9]{11,30}$", filtered_value, re.IGNORECASE) + and _mod_check(filtered_value) + ) + if filtered_value else False ) diff --git a/tests/test_iban.py b/tests/test_iban.py index e3b0798f..3e9688aa 100644 --- a/tests/test_iban.py +++ b/tests/test_iban.py @@ -7,7 +7,15 @@ from validators import ValidationError, iban -@pytest.mark.parametrize("value", ["GB82WEST12345698765432", "NO9386011117947"]) +@pytest.mark.parametrize( + "value", + [ + "GB82WEST12345698765432", + "NO9386011117947", + "GB 82WE ST12 3456 9876 5432", + "NO 9386 0111 1794 7", + ], +) def test_returns_true_on_valid_iban(value: str): """Test returns true on valid iban.""" assert iban(value)