-
Notifications
You must be signed in to change notification settings - Fork 70
✨ adding new type iban #65
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
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0dcf2ce
:sparkles: adding new type iban
kroncatti 8bf8b28
removing comment for type and changing from dict to any in property bank
kroncatti a492d54
♻️ more tests
yezz123 26517c2
:bug: remove invalid test
yezz123 6337187
🚨 fix linting issue
yezz123 0f39267
🚧 skip this for the moment
yezz123 ab85643
:ambulance: adding remaining properties
kroncatti c09bcda
:ambulance: fixing type-hint
kroncatti 2ab6ad6
:ambulance: adding remaining property, test and broadening lib version
kroncatti e095812
♻️ Update dependencies and fix type hinting in `IBAN` class
yezz123 308faa2
:bug: Update import statement for `IBAN` class
yezz123 99f145b
:bug: Update `schwifty` version in requirements
yezz123 79d1ab2
:memo: add documentation to Iban
yezz123 b77c320
Merge branch 'main' into type/iban
yezz123 a15766a
♻️ update `schwifty` version
yezz123 ed611a0
:bug: Update dependencies in pyproject.txt
yezz123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
""" | ||
The `pydantic_extra_types.iban` module provides functionality to recieve and validate [IBAN (International Bank Account Number)](https://en.wikipedia.org/wiki/International_Bank_Account_Number). | ||
""" | ||
|
||
|
||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
from pydantic import GetCoreSchemaHandler | ||
from pydantic_core import PydanticCustomError, core_schema | ||
|
||
try: | ||
import schwifty | ||
except ModuleNotFoundError: # pragma: no cover | ||
raise RuntimeError( | ||
'The `iban` module requires "schwifty" to be installed. You can install it with "pip install schwifty".' | ||
) | ||
|
||
|
||
class Iban(str): | ||
"""Represents a IBAN and provides methods for conversion, validation, and serialization. | ||
|
||
|
||
```py | ||
from pydantic import BaseModel, constr | ||
from pydantic_extra_types.iban import Iban | ||
class IbanExample(BaseModel): | ||
name: constr(strip_whitespace=True, min_length=1) | ||
number: Iban | ||
iban = IbanExample( | ||
name='Georg Wilhelm Friedrich Hegel', | ||
number='DE89 3704 0044 0532 0130 00', | ||
) | ||
assert iban.number.account_code == '0532013000' | ||
assert iban.number.bank_code == '37040044' | ||
assert iban.number.numeric == 370400440532013000131489 | ||
assert iban.number.bic == 'COBADEFFXXX' | ||
assert iban.number.bank_name == 'Commerzbank' | ||
assert iban.number.bban == '370400440532013000' | ||
``` | ||
""" | ||
|
||
def __init__(self, iban: str): | ||
self.iban = self.validate_iban_digits(iban) | ||
|
||
@classmethod | ||
def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaHandler) -> core_schema.CoreSchema: | ||
return core_schema.with_info_after_validator_function( | ||
cls._validate, | ||
core_schema.str_schema(), | ||
) | ||
|
||
@classmethod | ||
def _validate(cls, __input_value: str, _: core_schema.ValidationInfo) -> str: | ||
return cls(__input_value) | ||
|
||
@classmethod | ||
def validate_iban_digits(cls, iban: str) -> schwifty.iban.IBAN: | ||
"""Validate that the IBAN is all digits.""" | ||
if not isinstance(iban, str): | ||
raise PydanticCustomError('iban_digits', 'IBAN is invalid') # pragma: no cover | ||
return schwifty.IBAN(iban) | ||
|
||
@property | ||
def bank(self) -> Any: | ||
"""The bank of the IBAN.""" | ||
return self.iban.bank | ||
|
||
@property | ||
def compact(self) -> str: | ||
"""The compact IBAN.""" | ||
return self.iban.compact | ||
|
||
@property | ||
def formatted(self) -> str: | ||
"""The formatted IBAN.""" | ||
return self.iban.formatted | ||
|
||
@property | ||
def account_code(self) -> str: | ||
"""The account code of the IBAN.""" | ||
return self.iban.account_code | ||
|
||
@property | ||
def bank_code(self) -> str: | ||
"""The bank code of the IBAN.""" | ||
return self.iban.bank_code | ||
|
||
@property | ||
def numeric(self) -> int: | ||
"""The numeric IBAN.""" | ||
return self.iban.numeric | ||
|
||
@property | ||
def spec(self) -> Any: | ||
"""The IBAN spec.""" | ||
return self.iban.spec | ||
|
||
@property | ||
def bic(self) -> None | schwifty.bic.BIC: | ||
"""The BIC of the IBAN.""" | ||
return self.iban.bic | ||
|
||
@property | ||
def country(self) -> Any: | ||
"""The country of the IBAN.""" | ||
return self.iban.country | ||
|
||
@property | ||
def bank_name(self) -> None | str: | ||
"""The bank name of the IBAN.""" | ||
return self.iban.bank_name | ||
|
||
@property | ||
def bank_short_name(self) -> None | str: | ||
"""The bank short name of the IBAN.""" | ||
return self.iban.bank_short_name | ||
|
||
@property | ||
def branch_code(self) -> str: | ||
"""The branch code of the IBAN.""" | ||
return self.iban.branch_code | ||
|
||
@property | ||
def bban(self) -> str: | ||
"""The BBAN of the IBAN.""" | ||
return self.iban.bban | ||
|
||
@property | ||
def checksum_digits(self) -> str: | ||
"""The checksum digits of the IBAN.""" | ||
return self.iban.checksum_digits |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from string import printable | ||
|
||
import pytest | ||
from pydantic import BaseModel, ValidationError | ||
|
||
from pydantic_extra_types.iban import Iban | ||
|
||
|
||
@pytest.fixture(scope='module', name='IBANFixture') | ||
def iban_fixture(): | ||
class IBANFixture(BaseModel): | ||
made_in: Iban | ||
|
||
return IBANFixture | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'iban', | ||
[ | ||
'DE89 3704 0044 0532 0130 00', | ||
'DE89370400440532013000', | ||
'DE89370400440532013000', | ||
'NL56ABNA2238591354', | ||
'GB64BARC20040149326928', | ||
], | ||
) | ||
def test_iban_properties(iban, IBANFixture): | ||
iban_obj = IBANFixture(made_in=iban).made_in | ||
|
||
assert iban_obj.bank == iban_obj.iban.bank | ||
assert iban_obj.compact == iban_obj.iban.compact | ||
assert iban_obj.formatted == iban_obj.iban.formatted | ||
assert iban_obj.account_code == iban_obj.iban.account_code | ||
assert iban_obj.bank_code == iban_obj.iban.bank_code | ||
assert iban_obj.numeric == iban_obj.iban.numeric | ||
assert iban_obj.spec == iban_obj.iban.spec | ||
assert iban_obj.bic == iban_obj.iban.bic | ||
assert iban_obj.country == iban_obj.iban.country | ||
assert iban_obj.bank_name == iban_obj.iban.bank_name | ||
assert iban_obj.bank_short_name == iban_obj.iban.bank_short_name | ||
assert iban_obj.branch_code == iban_obj.iban.branch_code | ||
assert iban_obj.bban == iban_obj.iban.bban | ||
assert iban_obj.checksum_digits == iban_obj.iban.checksum_digits | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'iban', | ||
list(printable), | ||
) | ||
def test_invalid_iban(iban, IBANFixture): | ||
with pytest.raises(ValidationError, match='Invalid characters in IBAN'): | ||
IBANFixture(made_in=iban) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.