Validate form fields

Write codeEasy16 min

Your task

A checkout form moves focus to an error summary so a screen reader announces every problem at once, and that summary is built from field data alone. Three parallel lists arrive in document order: field_ids, rules and values. A rules entry is a comma separated list with no spaces, and it may be empty. The rules are required, email, digits and minlen:N. Trim a value before testing it. A blank value fails required, and every other rule is skipped on a blank value, because an optional field left alone is not an error. email needs exactly one at sign with text on both sides, digits allows only the characters 0 to 9, and minlen:N needs at least N characters. Report a failing field once, by the first rule it fails in the order that field lists them, as the field id, a vertical bar, then the rule exactly as written. Keep the given field order.

Examples

form_error_summary(field_ids: list[str], rules: list[str], values: list[str]) -> list[str]
Example 1
Inputform_error_summary(["full_name", "work_email", "zip_code"], ["required", "required,email", "digits,minlen:5"], [" ", "ada@example.com", "1234"])
Output["full_name|required", "zip_code|minlen:5"]
Example 2
Inputform_error_summary(["phone", "referral"], ["digits", "email"], ["", " "])
Output[]