Required
Ensure Field Presence and Non-Emptiness
The Required rule in the DataGuard library ensures that a specified field is present in the input data and is not empty. A field is considered "empty" if it meets any of the following conditions:
The value is
None.The value is an empty string (
"").The value is an empty list (
[]) or an empty countable object like a dictionary with no items ({}).
This rule is essential for validating fields that must contain data, preventing null or empty values from passing through the validation process.
from data_guard.validator import Validator
# Define data to be validated
data = {'name': None}
# Define validation rules
rules = {'name': ['required']}
# Initialize the validator
validator = Validator(data, rules)
# Perform validation
response = validator.validate()
if response.validated:
print("Validation passed!", response.data)
else:
# Output: {'email': ['The name field is required.']}
print("Validation failed with errors:", response.errors)Overview:
Purpose: Ensures a field is not empty.
Checks: Validates against
None, empty strings, and empty collections.Result: If the field is empty, an error message indicating that the field is required is generated.
Last updated