Skip to content
You're viewing docs for Dev. See the latest version →

Pydantic Extra Types Payment

The pydantic_extra_types.payment module provides the PaymentCardNumber data type.

PaymentCardBrand

Bases: str, Enum

Payment card brands supported by the PaymentCardNumber.

PaymentCardNumber

Bases: str

A payment card number.

Attributes

strip_whitespace

Whether to strip whitespace from the input value.

Type: bool Default: True

min_length

The minimum length of the card number.

Type: int Default: 12

max_length

The maximum length of the card number.

Type: int Default: 19

bin

The first 6 digits of the card number.

Type: str Default: card_number[:6]

last4

The last 4 digits of the card number.

Type: str Default: card_number[(-4):]

brand

The brand of the card.

Type: PaymentCardBrand Default: self.validate_brand(card_number)

masked

The masked card number.

Type: str

Methods

validate

@classmethod

def validate(
    cls,
    __input_value: str,
    _: core_schema.ValidationInfo,
) -> PaymentCardNumber

Validate the PaymentCardNumber instance.

Returns

PaymentCardNumber — The validated PaymentCardNumber instance.

Parameters

__input_value : str

The input value to validate.

_ : core_schema.ValidationInfo

The validation info.

validate_digits

@classmethod

def validate_digits(cls, card_number: str) -> None

Validate that the card number is all digits.

Returns

None

Parameters

card_number : str

The card number to validate.

Raises
  • PydanticCustomError — If the card number is not all digits.

validate_luhn_check_digit

@classmethod

def validate_luhn_check_digit(cls, card_number: str) -> str

Validate the payment card number. Based on the Luhn algorithm.

Returns

str — The validated card number.

Parameters

card_number : str

The card number to validate.

Raises
  • PydanticCustomError — If the card number is not valid.

validate_brand

@staticmethod

def validate_brand(card_number: str) -> PaymentCardBrand

Validate length based on BIN for major brands.

Returns

PaymentCardBrand — The validated card brand.

Parameters

card_number : str

The card number to validate.

Raises
  • PydanticCustomError — If the card number is not valid.