Skip to content

Commit

Permalink
lower case currency is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
edasubert committed Nov 11, 2024
1 parent f27a132 commit 21c1a67
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
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

0 comments on commit 21c1a67

Please sign in to comment.