From 21c1a67fe3091c59ace475f07b17eb83aee63976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduard=20=C5=A0ubert?= <6988280+edasubert@users.noreply.github.com> Date: Mon, 11 Nov 2024 07:21:08 +0000 Subject: [PATCH] lower case currency is valid --- pydantic_extra_types/currency_code.py | 2 ++ tests/test_currency_code.py | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/pydantic_extra_types/currency_code.py b/pydantic_extra_types/currency_code.py index 7767ae0..89c12da 100644 --- a/pydantic_extra_types/currency_code.py +++ b/pydantic_extra_types/currency_code.py @@ -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' @@ -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', diff --git a/tests/test_currency_code.py b/tests/test_currency_code.py index 78f54fe..c3ca4d0 100644 --- a/tests/test_currency_code.py +++ b/tests/test_currency_code.py @@ -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( @@ -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,