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

Validate character set for BICs #364

Merged
merged 3 commits into from
Mar 3, 2019
Merged
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
1 change: 1 addition & 0 deletions docs/authors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Authors
* Peter J. Farrell
* Rael Max
* Ramiro Morales
* Raphael Michel
* Rolf Erik Lekang
* Russell Keith-Magee
* Serafeim Papastefanos
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Other changes:

- Added support for Vatican IBAN

- Extended validation of BICs to check for the correct character set


2.1 (2018-08-24)
------------------
Expand Down
2 changes: 1 addition & 1 deletion localflavor/generic/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def to_python(self, value):
value = super(BICFormField, self).to_python(value)
if value in self.empty_values:
return self.empty_value
return value.upper()
return value.upper().replace(" ", "")

def prepare_value(self, value):
# BIC is always written in upper case.
Expand Down
6 changes: 5 additions & 1 deletion localflavor/generic/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ def __call__(self, value):
if bic_length != 8 and bic_length != 11:
raise ValidationError(_('BIC codes have either 8 or 11 characters.'))

# BIC is alphanumeric
if any(char not in string.ascii_uppercase + string.digits for char in value):
raise ValidationError(_('BIC codes only contain alphabet letters and digits.'))

# First 4 letters are A - Z.
institution_code = value[:4]
for x in institution_code:
Expand Down Expand Up @@ -314,7 +318,7 @@ def __call__(self, value):
Length of the country code prefix of a VAT identification number.

Codes are two letter ISO 3166-1 alpha-2 codes except for Greece that uses
ISO 639-1.
ISO 639-1.
"""


Expand Down
4 changes: 3 additions & 1 deletion tests/test_generic/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ def test_bic_validator(self):
'NEDSZAJJXX': 'BIC codes have either 8 or 11 characters.',
'': 'BIC codes have either 8 or 11 characters.',
'CIBCJJH2': 'JJ is not a valid country code.',
'DÉUTDEFF': 'is not a valid institution code.'
'D3UTDEFF': 'is not a valid institution code.',
'DÉUTDEFF': 'codes only contain alphabet letters and digits.',
'NEDSZAJJ XX': 'codes only contain alphabet letters and digits.',
}

bic_validator = BICValidator()
Expand Down