app/main/validators.py.
Password Validators
CommonlyUsedPassword
Prevents use of commonly compromised passwords.message(str, optional): Custom error message
app/main/_commonly_used_passwords.py
Email Validators
ValidEmail
Validates email address format using notifications_utils.message(str, optional): Field-level error messageerror_summary_message(str, optional): Error summary message format (default: “Enter %s in the correct format”)
- Validates format using
notifications_utils.recipient_validation.email_address - Appends error summary messages if field has
error_summary_messagesattribute - Returns early if field is empty (allows optional fields)
ValidGovEmail
Requires a public sector email address.- Checks email against government domains list
- Checks email against approved organisation domains
- Returns early if field is empty
app.utils.user.is_gov_user() function
Phone Number Validators
ValidPhoneNumber
Validates phone numbers with configurable options.allow_international_sms(bool): Allow international numbers (default: False)allow_sms_to_uk_landlines(bool): Allow UK landlines (default: False)message(str, optional): Custom error message
| Error Code | Message Format |
|---|---|
| TOO_SHORT | ”%s is too short” |
| TOO_LONG | ”%s is too long” |
| NOT_A_UK_MOBILE | ”%s does not look like a UK mobile number” |
| UNSUPPORTED_COUNTRY_CODE | ”Country code for %s not found” |
| UNKNOWN_CHARACTER | ”%s can only include: 0 1 2 3 4 5 6 7 8 9 ( ) + -“ |
| INVALID_NUMBER | ”%s is not valid – double check the phone number you entered” |
- Uses
notifications_utils.recipient_validation.phone_number.PhoneNumber - Appends error summary messages if field supports it
- Returns early if field is empty
Template Validators
NoCommasInPlaceHolders
Prevents commas in template placeholders.message(str, optional): Custom error message
Field(field.data).placeholders contains a comma
OnlySMSCharacters
Ensures SMS template only contains compatible characters.template_type(str): Template type (passed as keyword argument)
- Uses
notifications_utils.sanitise_text.SanitiseSMS.get_non_compatible_characters() - Provides helpful message about display issues
- Lists all problematic characters found
SMS Sender ID Validators
DoesNotStartWithDoubleZero
Prevents sender IDs starting with “00”.message(str, optional): Custom error message
IsNotAGenericSenderID
Blocks generic sender IDs commonly used by spam.message(str, optional): Custom error message
IsNotLikeNHSNoReply
Enforces correct NHS no-reply sender ID format.- If sender ID contains “nhs”, “no”, and “reply” (case-insensitive)
- And is not exactly “NHSNoReply”
- Then raises error
IsNotAPotentiallyMaliciousSenderID
Checks sender ID against phishing protection list.- Checks against protected sender ID API
- Creates Zendesk ticket if malicious sender ID detected
- Logs warning with sender ID details
- Creates Zendesk support ticket
- Logs security warning
IsAUKMobileNumberOrShortCode
Validates numeric sender IDs as UK mobile or shortcode.- UK mobile:
07[0-9]{9}(e.g., “07700900000”) - Shortcode:
[6-8][0-9]{4}(e.g., “60000”)
message(str, optional): Custom error message
Content Validators
MustContainAlphanumericCharacters
Requires at least two alphanumeric characters.thing(str, optional): Name of field for error messagemessage(str, optional): DEPRECATED - usethinginstead
- With
thing: ” must include at least 2 letters or numbers” - With
message: Custom message - Default: “Must include at least two alphanumeric characters”
.*[a-zA-Z0-9].*[a-zA-Z0-9].*
CharactersNotAllowed
Blocks specific characters from input.characters_not_allowed(iterable): Characters to blockthing(str): Name of field (default: “item”)message(str, optional): Custom field error messageerror_summary_message(str, optional): Custom summary error message
- Field: “Cannot contain ”
- Summary: “%s cannot contain ”
- Uses
OrderedSetfor consistent ordering - Formats character list with “or” conjunction
- Appends to error_summary_messages if available
StringsNotAllowed
Blocks specific strings or substrings.*args(str): Strings to block (case-insensitive)thing(str): Name of field (default: “item”)message(str, optional): Custom field error messageerror_summary_message(str, optional): Custom summary error messagematch_on_substrings(bool): Match anywhere in string (default: False)
- Exact match: “Cannot be ''”
- Substring match: “Cannot contain ''”
- Summary: “%s cannot be/contain ''”
- Case-insensitive matching
- Can match exact strings or substrings
- Normalizes strings to lowercase for comparison
CannotContainURLsOrLinks
Prevents URLs and markdown links in content.thing(str): Name of field for error message
- Checks content through
autolink_urls()andnotify_email_markdown() - If either produces
<a href=, raises error - Catches URLs, markdown links, and other link formats
File Validators
CsvFileValidator
Validates CSV and spreadsheet uploads.message(str, optional): Custom error message (not used in current implementation)
Spreadsheet.can_handle(filename) to check if file type is supported
Supported Formats: CSV, XLS, XLSX, ODS, etc. (as defined in Spreadsheet model)
NoBracketsInFileName
Prevents brackets in filenames.( or ) in field.data.filename
DocumentDownloadFileValidator
Validates file extensions against allowed list.message(str, optional): Custom error message
- With extension:
{.ext} is not an allowed file format - Without extension: Uses default message
- Form must have
allowed_file_extensionsattribute - Extensions checked case-insensitively
- Leading dot stripped from extension before checking
FileIsVirusFree
Scans uploaded files for viruses.- Only runs if
ANTIVIRUS_ENABLEDconfig is True - Uses
antivirus_client.scan(field.data) - Resets file pointer after scan
- Raises
StopValidationto prevent further processing
- Scans file data stream
- Returns to start of file after scan (via
seek(0)) - Stops validation chain if virus detected
SVG Validators
Base class and implementations for SVG file validation.NoElementInSVG (Abstract Base Class)
Base class for SVG element validators.element(str): Element name to check formessage(str): Error message
- Reads SVG file contents
- Checks if
<{element}appears in file (case-insensitive) - Resets file pointer after check
NoEmbeddedImagesInSVG
Prevents embedded raster images in SVG files.image
Message: “This SVG has an embedded raster image in it and will not render well”
Purpose: Ensures SVGs only use vector graphics for quality and performance
NoTextInSVG
Prevents unconverted text in SVG files.text
Message: “This SVG has text which has not been converted to paths and may not render well”
Purpose: Prevents font rendering issues by requiring text to be converted to vector paths
Standard WTForms Validators (Extended)
NotifyDataRequired
Custom version of WTForms’ DataRequired with better messaging.thing(str): Name of required field
NotifyDataRequired(thing="your email address") → “Enter your email address”
NotifyInputRequired
Custom version of WTForms’ InputRequired with better messaging.thing(str): Name of required field
NotifyUrlValidator
Custom version of WTForms’ URL validator with better messaging.thing(str): Description of URL (default: “a URL in the correct format”)
Length
Extended version of WTForms’ Length with automatic message generation.min(int): Minimum length (default: -1, no minimum)max(int): Maximum length (default: -1, no maximum)thing(str): Name of field (required unless message provided)unit(str): Unit of measurement (default: “characters”)message(str, optional): Override automatic message
- Min and max equal: ” must be long”
- Min and max both set: ” must be between and long”
- Only min set: ” must be at least long”
- Only max set: ” cannot be longer than ”