PaymentCardNumber
This class represents a payment card number and provides comprehensive validation including digit checks, Luhn algorithm verification, and brand-specific length requirements. It automatically extracts metadata such as the Bank Identification Number (BIN), the last four digits, and the card brand during initialization. Additionally, it includes a property for generating a masked version of the card number for secure display.
Attributes
| Attribute | Type | Description |
|---|---|---|
| strip_whitespace | ClassVar[bool] = True | Determines whether leading and trailing whitespace should be removed from the card number string during validation. |
| min_length | ClassVar[int] = 12 | The minimum allowed character length for a valid payment card number. |
| max_length | ClassVar[int] = 19 | The maximum allowed character length for a valid payment card number. |
| bin | str | The Bank Identification Number (BIN) consisting of the first six digits of the card number. |
| last4 | str | The final four digits of the payment card number used for identification and display. |
| brand | [PaymentCardBrand](../v1/types/paymentcardbrand.md?sid=pydantic_v1_types_paymentcardbrand) | The detected issuer brand of the payment card based on its BIN and length. |
Constructor
Signature
def PaymentCardNumber(
card_number: str
)
Parameters
| Name | Type | Description |
|---|---|---|
| card_number | str | The raw payment card number string to be validated and processed. |
Signature
def PaymentCardNumber(
card_number: str
)
Parameters
| Name | Type | Description |
|---|---|---|
| card_number | str | The raw payment card number string to be validated and parsed |
Methods
validate()
@classmethod
def validate(
input_value: str,
_: core_schema.ValidationInfo
) - > [PaymentCardNumber](../v1/types/paymentcardnumber.md?sid=pydantic_v1_types_paymentcardnumber)
Validate the card number and return a PaymentCardNumber instance.
Parameters
| Name | Type | Description |
|---|---|---|
| input_value | str | The raw card number string to validate |
| _ | core_schema.ValidationInfo | Pydantic validation context information |
Returns
| Type | Description |
|---|---|
[PaymentCardNumber](../v1/types/paymentcardnumber.md?sid=pydantic_v1_types_paymentcardnumber) | An initialized and validated PaymentCardNumber instance |
masked()
@classmethod
def masked() - > str
Mask all but the last 4 digits of the card number.
Returns
| Type | Description |
|---|---|
str | A masked card number string. |
validate_digits()
@classmethod
def validate_digits(
card_number: str
) - > None
Validate that the card number is all digits.
Parameters
| Name | Type | Description |
|---|---|---|
| card_number | str | The card number string to check for numeric characters |
Returns
| Type | Description |
|---|---|
None | Nothing; raises a PydanticCustomError if non-digit characters are found |
validate_luhn_check_digit()
@classmethod
def validate_luhn_check_digit(
card_number: str
) - > str
Based on: https://en.wikipedia.org/wiki/Luhn_algorithm.
Parameters
| Name | Type | Description |
|---|---|---|
| card_number | str | The card number string to verify using the Luhn algorithm |
Returns
| Type | Description |
|---|---|
str | The validated card number string if the Luhn check passes |
validate_brand()
@classmethod
def validate_brand(
card_number: str
) - > [PaymentCardBrand](../v1/types/paymentcardbrand.md?sid=pydantic_v1_types_paymentcardbrand)
Validate length based on BIN for major brands: https://en.wikipedia.org/wiki/Payment_card_number#Issuer_identification_number_(IIN).
Parameters
| Name | Type | Description |
|---|---|---|
| card_number | str | The card number string used to identify the issuer and validate length |
Returns
| Type | Description |
|---|---|
[PaymentCardBrand](../v1/types/paymentcardbrand.md?sid=pydantic_v1_types_paymentcardbrand) | The identified PaymentCardBrand enum value |