Angreal templates can now include validation rules for user input, ensuring that template variables meet specific criteria. This guide explains how to add validation rules to your templates.
Validation rules are defined in the [validation]
section of your angreal.toml
file using dotted notation:
[validation]
variable_name.validation_method = validation_parameters
The allowed_values
method validates that user input is one of the specified values:
[validation]
role.allowed_values = ["admin", "user", "guest"]
python_version.allowed_values = ["3.8", "3.9", "3.10", "3.11"]
The min
and max
methods validate that numeric inputs are within a specific range:
[validation]
# Age must be between 18 and 65
age.min = 18
age.max = 65
# Temperature must be positive
temperature.min = 0
# Percentage must be at most 100
percentage.max = 100
The regex_match
method validates that inputs match a regular expression pattern:
[validation]
# Validate email format
email.regex_match = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
# Validate phone number format
phone.regex_match = "^\\d{3}-\\d{3}-\\d{4}$"
# Validate alphanumeric input
username.regex_match = "^[a-zA-Z0-9]+$"
The not_empty
method ensures that a field is not left empty:
[validation]
# Name is required
name.not_empty = true
# Description is optional
description.not_empty = false
The type
method validates that input can be parsed as a specific data type:
[validation]
# Must be an integer
age.type = "integer"
# Must be a float/number
price.type = "float"
# Must be a boolean (true/false)
active.type = "boolean"
# Must be a string (always passes)
name.type = "string"
The length_min
and length_max
methods validate the length of string inputs:
[validation]
# Username between 3 and 20 characters
username.length_min = 3
username.length_max = 20
# Password at least 8 characters
password.length_min = 8
# Code must be exactly 6 characters
code.length_min = 6
code.length_max = 6
Here’s an example of an angreal.toml
file with validation rules:
# Template variables
project_name = "my_project"
author = "Developer"
role = "user"
python_version = "3.9"
age = 30
email = "developer@example.com"
score = 75
username = "user123"
password = ""
# Custom prompts
[prompt]
project_name = "What is the name of your project?"
author = "Who is the main author/maintainer?"
role = "Select a role (admin, user, guest)"
python_version = "Which Python version will this project use?"
age = "Enter your age (must be between 18 and 65)"
email = "Enter your email address"
score = "Enter a score (0, 25, 50, 75, or 100)"
username = "Choose a username (3-20 characters, alphanumeric)"
password = "Create a password (min 8 characters)"
# Validation rules
[validation]
# Basic value validation
role.allowed_values = ["admin", "user", "guest"]
python_version.allowed_values = ["3.8", "3.9", "3.10", "3.11"]
# Numeric validation
age.min = 18
age.max = 65
age.type = "integer"
score.allowed_values = [0, 25, 50, 75, 100]
# Format validation
email.regex_match = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
email.not_empty = true
# String length validation
username.length_min = 3
username.length_max = 20
username.regex_match = "^[a-zA-Z0-9]+$"
password.length_min = 8
password.not_empty = true
When a user runs angreal init
with a template that includes validation rules:
For the best user experience, combine validation rules with custom prompts:
# Variable with default
role = "user"
# User-friendly prompt
[prompt]
role = "Select a role (admin, user, guest)"
# Validation rule
[validation]
role.allowed_values = ["admin", "user", "guest"]
This approach: