Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Apply multiple validation rules and return all failures

Validate a user-like record against several rules and return all failing messages. Learn to collect and report multiple validation errors deterministically.

Python practice25 minError Handling & Edge CasesAdvancedLast updated March 20, 2026

Problem statement

You will write a function validate_record(record) that accepts a dictionary representing a user-like record and returns a list of validation error messages. The validator must apply multiple checks for each field and report every failure found, not just the first. Order of messages must be deterministic so tests are reliable. Fields to validate: - username (required): string, 3-20 characters, only alphanumeric characters and underscores. - age (required): integer, between 0 and 120 inclusive. - email (required): string containing exactly one '@'; the domain part (after '@') must contain at least one '.'. - password (required): string, at least 8 characters, contains at least one digit, one uppercase letter, and one lowercase letter. - tags (optional): if present, must be a list of non-empty strings and must not contain duplicates. Additionally, any keys in the input not in the allowed set {username, age, email, password, tags} should be reported as unexpected fields. The function should return a list of error message strings in a fixed order (see examples). If there are no validation failures, return an empty list. Be defensive: ensure type checks are done before applying type-specific validations to avoid exceptions.

Task

Implement a robust validator that applies multiple checks to a dictionary record and returns a list of all validation failures (no short-circuiting).

Examples

Valid record

Input

{'username':'dev_user','age':30,'email':'dev@example.com','password':'Strong1Pass','tags':['python','dev']}

Output

[]

All fields meet the validation rules, so an empty list is returned.

Input format

A single argument: record — a Python dict representing the user record.

Output format

A list of strings. Each string describes a specific validation failure. Return an empty list if the record is valid.

Constraints

Do not raise exceptions for invalid input; collect and return validation messages. Keep message strings exactly as specified so tests can compare deterministically.

Samples

Sample 1

Input

{'username':'ab','age':25,'email':'no-at-symbol','password':'weak','tags':['a','']}

Output

['username: must be 3-20 characters', 'email: must contain single @', 'password: must be at least 8 characters', 'password: must include a digit', 'password: must include uppercase letter', 'tags: tags must be non-empty strings']

Multiple rules fail; all failures are returned in a predictable order.