Skip to content

Commit c0ad1b5

Browse files
authored
🐛 fix issue with Lower case Currency (#236)
1 parent f27a132 commit c0ad1b5

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

pydantic_extra_types/currency_code.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def _validate(cls, currency_code: str, _: core_schema.ValidationInfo) -> str:
6868
Raises:
6969
PydanticCustomError: If the ISO 4217 currency code is not valid.
7070
"""
71+
currency_code = currency_code.upper()
7172
if currency_code not in cls.allowed_currencies:
7273
raise PydanticCustomError(
7374
'ISO4217', 'Invalid ISO 4217 currency code. See https://en.wikipedia.org/wiki/ISO_4217'
@@ -128,6 +129,7 @@ def _validate(cls, currency_symbol: str, _: core_schema.ValidationInfo) -> str:
128129
Raises:
129130
PydanticCustomError: If the ISO 4217 currency code is not valid or is bond, precious metal or testing code.
130131
"""
132+
currency_symbol = currency_symbol.upper()
131133
if currency_symbol not in cls.allowed_currencies:
132134
raise PydanticCustomError(
133135
'InvalidCurrency',

tests/test_currency_code.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ def test_ISO4217_code_ok(currency: str):
2525
assert model.model_dump() == {'currency': currency} # test serialization
2626

2727

28+
@pytest.mark.parametrize('currency', ['USD', 'usd', 'UsD'])
29+
def test_ISO4217_code_ok_lower_case(currency: str):
30+
model = ISO4217CheckingModel(currency=currency)
31+
assert model.currency == currency.upper()
32+
33+
2834
@pytest.mark.parametrize(
2935
'currency',
3036
filter(
@@ -38,6 +44,12 @@ def test_everyday_code_ok(currency: str):
3844
assert model.model_dump() == {'currency': currency} # test serialization
3945

4046

47+
@pytest.mark.parametrize('currency', ['USD', 'usd', 'UsD'])
48+
def test_everyday_code_ok_lower_case(currency: str):
49+
model = CurrencyCheckingModel(currency=currency)
50+
assert model.currency == currency.upper()
51+
52+
4153
def test_ISO4217_fails():
4254
with pytest.raises(
4355
ValidationError,

0 commit comments

Comments
 (0)