mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
fix card number length check in PaymentCardNumber (#1321)
* fix card number length check in PaymentCardNumber * add check for PaymentCardBrand str()
This commit is contained in:
@@ -0,0 +1 @@
|
||||
fix card number length check in `PaymentCardNumber`, `PaymentCardBrand` now inherits from `str`.
|
||||
+2
-2
@@ -591,7 +591,7 @@ class SecretBytes:
|
||||
return self._secret_value
|
||||
|
||||
|
||||
class PaymentCardBrand(Enum):
|
||||
class PaymentCardBrand(str, Enum):
|
||||
amex = 'American Express'
|
||||
mastercard = 'Mastercard'
|
||||
visa = 'Visa'
|
||||
@@ -666,7 +666,7 @@ class PaymentCardNumber(str):
|
||||
https://en.wikipedia.org/wiki/Payment_card_number#Issuer_identification_number_(IIN)
|
||||
"""
|
||||
required_length: Optional[int] = None
|
||||
if card_number.brand is (PaymentCardBrand.visa or PaymentCardBrand.mastercard):
|
||||
if card_number.brand in {PaymentCardBrand.visa, PaymentCardBrand.mastercard}:
|
||||
required_length = 16
|
||||
valid = len(card_number) == required_length
|
||||
elif card_number.brand is PaymentCardBrand.amex:
|
||||
|
||||
@@ -78,6 +78,7 @@ def test_validate_luhn_check_digit(card_number: str, valid: bool):
|
||||
(VALID_AMEX, PaymentCardBrand.amex, True),
|
||||
(VALID_OTHER, PaymentCardBrand.other, True),
|
||||
(LEN_INVALID, PaymentCardBrand.visa, False),
|
||||
(VALID_AMEX, PaymentCardBrand.mastercard, False),
|
||||
],
|
||||
)
|
||||
def test_length_for_brand(card_number: str, brand: PaymentCardBrand, valid: bool):
|
||||
@@ -123,3 +124,21 @@ def test_error_types(card_number: Any, error_message: str):
|
||||
with pytest.raises(ValidationError, match=error_message) as exc_info:
|
||||
PaymentCard(card_number=card_number)
|
||||
assert exc_info.value.json().startswith('[')
|
||||
|
||||
|
||||
def test_payment_card_brand():
|
||||
b = PaymentCardBrand.visa
|
||||
assert str(b) == 'Visa'
|
||||
assert b is PaymentCardBrand.visa
|
||||
assert b == PaymentCardBrand.visa
|
||||
assert b in {PaymentCardBrand.visa, PaymentCardBrand.mastercard}
|
||||
|
||||
b = 'Visa'
|
||||
assert b is not PaymentCardBrand.visa
|
||||
assert b == PaymentCardBrand.visa
|
||||
assert b in {PaymentCardBrand.visa, PaymentCardBrand.mastercard}
|
||||
|
||||
b = PaymentCardBrand.amex
|
||||
assert b is not PaymentCardBrand.visa
|
||||
assert b != PaymentCardBrand.visa
|
||||
assert b not in {PaymentCardBrand.visa, PaymentCardBrand.mastercard}
|
||||
|
||||
Reference in New Issue
Block a user