Payment Card Numbers


To use this type, you need to install the optional pydantic-extra-types package:

pip install pydantic-extra-types

The PaymentCardNumber type validates payment cards (such as a debit or credit card).

from datetime import date

from pydantic import BaseModel, constr

from pydantic_extra_types.payment import PaymentCardBrand, PaymentCardNumber

class Card(BaseModel):
    name: constr(strip_whitespace=True, min_length=1)
    number: PaymentCardNumber
    exp: date

    def brand(self) -> PaymentCardBrand:
        return self.number.brand

    def expired(self) -> bool:
        return self.exp <

card = Card(
    name='Georg Wilhelm Friedrich Hegel',
    exp=date(2023, 9, 30),

assert card.number.brand ==
assert card.number.bin == '400000'
assert card.number.last4 == '0002'
assert card.number.masked == '400000******0002'

PaymentCardBrand can be one of the following based on the BIN:

  • PaymentCardBrand.mastercard
  • PaymentCardBrand.other

The actual validation verifies the card number is:

  • a str of only digits
  • luhn valid
  • the correct length based on the BIN, if Amex, Mastercard or Visa, and between 12 and 19 digits for all other brands