duck.utils.validationยถ

Validation Utilities Module

This module provides a set of validation functions to check various types of data commonly used in web development and application processing, such as strings, email addresses, passwords, IP addresses, credit cards, and more.

Functions include:

  • validate_email: Validates email address format.

  • validate_phone: Validates phone number format.

  • validate_url: Validates URL format.

  • validate_username: Validates username format.

  • validate_ip_address: Validates IP address (supports both IPv4 and IPv6).

  • validate_hex_color: Validates a hex color code.

  • validate_credit_card_type: Checks if the credit card belongs to a certain type (e.g., Visa, MasterCard).

  • validate_json: Validates if a string is a valid JSON.

  • validate_hexadecimal: Validates if the string is a valid hexadecimal number.

  • validate_base64: Validates if the string is a valid Base64 encoded string.

  • validate_password_strength: Validates if a password meets security requirements.

  • validate_time: Validates if a time string is in HH:MM format.

Module Contentsยถ

Functionsยถ

validate_base64

Validates whether the provided string is a valid Base64 encoded string.

validate_credit_card

Validates whether the provided credit card number is valid using the Luhn algorithm.

validate_credit_card_type

Validates the credit card type based on the number provided. Supported card types include: Visa, MasterCard, American Express, Discover.

validate_email

Validates the format of an email address.

validate_hex_color

Validates if the provided string is a valid hex color code.

validate_hexadecimal

Validates if the string is a valid hexadecimal number.

validate_ip_address

Validates whether the provided IP address is a valid IPv4 or IPv6 address.

validate_json

Validates whether a string is a valid JSON formatted string.

validate_password_strength

Validates the strength of a password. The password must:

validate_phone

Validates the format of a phone number (supports both international and local formats).

validate_time

Validates if the provided time string is in HH:MM format.

validate_url

Validates the format of a URL.

validate_username

Validates if the username is alphanumeric and contains only letters, numbers, and underscores. The length should be between 3 to 20 characters.

APIยถ

duck.utils.validation.validate_base64(text: str) โ†’ bool[source]ยถ

Validates whether the provided string is a valid Base64 encoded string.

Parameters:

text โ€“ The string to validate.

Returns:

True if the string is valid Base64, False otherwise.

Return type:

bool

duck.utils.validation.validate_credit_card(card_number: str) โ†’ bool[source]ยถ

Validates whether the provided credit card number is valid using the Luhn algorithm.

Parameters:

card_number โ€“ The credit card number to validate.

Returns:

True if the credit card number is valid, False otherwise.

Return type:

bool

duck.utils.validation.validate_credit_card_type(card_number: str) โ†’ Optional[str][source]ยถ

Validates the credit card type based on the number provided. Supported card types include: Visa, MasterCard, American Express, Discover.

Parameters:

card_number โ€“ The credit card number to validate.

Returns:

The type of the credit card (e.g., โ€˜Visaโ€™, โ€˜MasterCardโ€™, etc.), or None if invalid.

Return type:

str

duck.utils.validation.validate_email(email: str) โ†’ bool[source]ยถ

Validates the format of an email address.

Parameters:

email โ€“ The email address to validate.

Returns:

True if the email address is valid, False otherwise.

Return type:

bool

duck.utils.validation.validate_hex_color(color: str) โ†’ bool[source]ยถ

Validates if the provided string is a valid hex color code.

Parameters:

color โ€“ The color string to validate.

Returns:

True if the color is a valid hex color code, False otherwise.

Return type:

bool

duck.utils.validation.validate_hexadecimal(text: str) โ†’ bool[source]ยถ

Validates if the string is a valid hexadecimal number.

Parameters:

text โ€“ The string to validate.

Returns:

True if the string is a valid hexadecimal number, False otherwise.

Return type:

bool

duck.utils.validation.validate_ip_address(ip_address: str) โ†’ bool[source]ยถ

Validates whether the provided IP address is a valid IPv4 or IPv6 address.

Parameters:

ip_address โ€“ The IP address to validate.

Returns:

True if the IP address is valid, False otherwise.

Return type:

bool

duck.utils.validation.validate_json(data: str) โ†’ bool[source]ยถ

Validates whether a string is a valid JSON formatted string.

Parameters:

data โ€“ The string to validate.

Returns:

True if the string is valid JSON, False otherwise.

Return type:

bool

duck.utils.validation.validate_password_strength(password: str) โ†’ bool[source]ยถ

Validates the strength of a password. The password must:

  • Be at least 8 characters long.

  • Contain at least one lowercase letter.

  • Contain at least one uppercase letter.

  • Contain at least one number.

  • Contain at least one special character (e.g., !, @, #, $, etc.).

Parameters:

password โ€“ The password to validate.

Returns:

True if the password meets the strength requirements, False otherwise.

Return type:

bool

duck.utils.validation.validate_phone(phone: str) โ†’ bool[source]ยถ

Validates the format of a phone number (supports both international and local formats).

Parameters:

phone โ€“ The phone number to validate.

Returns:

True if the phone number is valid, False otherwise.

Return type:

bool

duck.utils.validation.validate_time(time_str: str) โ†’ bool[source]ยถ

Validates if the provided time string is in HH:MM format.

Parameters:

time_str โ€“ The time string to validate.

Returns:

True if the time string is valid, False otherwise.

Return type:

bool

duck.utils.validation.validate_url(url: str) โ†’ bool[source]ยถ

Validates the format of a URL.

Parameters:

url โ€“ The URL to validate.

Returns:

True if the URL is valid, False otherwise.

Return type:

bool

duck.utils.validation.validate_username(username: str) โ†’ bool[source]ยถ

Validates if the username is alphanumeric and contains only letters, numbers, and underscores. The length should be between 3 to 20 characters.

Parameters:

username โ€“ The username to validate.

Returns:

True if the username is valid, False otherwise.

Return type:

bool