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

lower case currency is valid #236

Merged
merged 1 commit into from
Nov 13, 2024
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
2 changes: 2 additions & 0 deletions pydantic_extra_types/currency_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def _validate(cls, currency_code: str, _: core_schema.ValidationInfo) -> str:
Raises:
PydanticCustomError: If the ISO 4217 currency code is not valid.
"""
currency_code = currency_code.upper()
if currency_code not in cls.allowed_currencies:
raise PydanticCustomError(
'ISO4217', 'Invalid ISO 4217 currency code. See https://en.wikipedia.org/wiki/ISO_4217'
Expand Down Expand Up @@ -128,6 +129,7 @@ def _validate(cls, currency_symbol: str, _: core_schema.ValidationInfo) -> str:
Raises:
PydanticCustomError: If the ISO 4217 currency code is not valid or is bond, precious metal or testing code.
"""
currency_symbol = currency_symbol.upper()
if currency_symbol not in cls.allowed_currencies:
raise PydanticCustomError(
'InvalidCurrency',
Expand Down
12 changes: 12 additions & 0 deletions tests/test_currency_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def test_ISO4217_code_ok(currency: str):
assert model.model_dump() == {'currency': currency} # test serialization


@pytest.mark.parametrize('currency', ['USD', 'usd', 'UsD'])
def test_ISO4217_code_ok_lower_case(currency: str):
model = ISO4217CheckingModel(currency=currency)
assert model.currency == currency.upper()


@pytest.mark.parametrize(
'currency',
filter(
Expand All @@ -38,6 +44,12 @@ def test_everyday_code_ok(currency: str):
assert model.model_dump() == {'currency': currency} # test serialization


@pytest.mark.parametrize('currency', ['USD', 'usd', 'UsD'])
def test_everyday_code_ok_lower_case(currency: str):
model = CurrencyCheckingModel(currency=currency)
assert model.currency == currency.upper()


def test_ISO4217_fails():
with pytest.raises(
ValidationError,
Expand Down
Loading