Email

Validate Email Format

The Email rule in the DataGuard library ensures that a given field contains a valid email address. This rule checks whether the field's value follows the standard email format, which typically includes an @ symbol and a domain name.

from data_guard.validator import Validator

# Define the data to be validated
data = {"email": "invalid-email"}

# Define validation rules
rules = {"email": ["email"]}

# Initialize the validator
validator = Validator(data, rules)

# Perform validation
response = validator.validate()

if response.validated:
    print("Validation passed!", response.data)
else:
    # Output: {'email': ['email must be a valid email address']}
    print("Validation failed with errors:", response.errors)

Overview

  • Purpose: Validates that a field contains a correctly formatted email address.

  • Checks: Ensures the presence of an @ symbol and a valid domain name in the field value.

  • Result: If the value is not a valid email, an error message indicating that the field must be a valid email address is generated.

Last updated