Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add whitespace filtering to iban validator #391

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/validators/iban.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- filtered_value = _filter_whitespace(value)
+ if not value:
+     return False
+ 
+ # Remove Unicode white-spaces from the string.
+ value = "".join(filter(lambda x: not x.isspace(), value))

I think the function call can be skipped.

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
)
10 changes: 9 additions & 1 deletion tests/test_iban.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down