Fix the accessibility audit for a sign-up email field
Your task
An AI built a sign-up email field with inline validation. The label and the red error show on screen, but neither is connected to the input in code. A screen reader announces "edit text" and never reads the error. Your team now audits the attributes each field really renders.
An assistant wrote audit_email_field(attrs). It is in the editor. It has a bug. Find it and fix it.
Input and output:
attrs: the field's rendered attributes, each writtenkey=value. The keys areid,type,placeholder,aria-label,aria-labelledby,aria-describedbyandaria-invalidon the input;label-for, theforof a<label>; anderror-id,error-roleanderror-aria-liveon the error message, when one is shown.- Return the defect codes found, sorted by plain character order.
Rules the tests check:
- Skip an entry with no
=. Split the rest at the first=and trim both sides. Skip an empty key. When a key repeats, its last value wins. A missing key counts as an empty value. - The input has a programmatic label when
label-foris not empty and equalsid, oraria-labelis not empty, oraria-labelledbyis not empty. A placeholder is not a label. "no-programmatic-label": the input has no programmatic label. In that case, also add"placeholder-not-a-label"whenplaceholderis not empty."wrong-input-type":typeis not exactlyemail.- When
error-idis not empty, an error is shown and three more checks apply:"error-not-described-by":error-idis not one of the space-separated ids inaria-describedby."error-not-announced":error-roleis notalert, anderror-aria-liveis neitherpolitenorassertive."missing-aria-invalid":aria-invalidis not exactlytrue.
Examples
audit_email_field(attrs: list[str]) -> list[str]Example 1
Input
audit_email_field(["id=email", "type=text", "placeholder=Email", "error-id=email-error"])Output
["error-not-announced", "error-not-described-by", "missing-aria-invalid", "no-programmatic-label", "placeholder-not-a-label", "wrong-input-type"]Example 2
Input
audit_email_field(["id=email", "type=email", "label-for=email", "aria-invalid=true", "aria-describedby=email-error", "error-id=email-error", "error-role=alert"])Output
[]Example 3
Input
audit_email_field(["id=email", "type=text", "label-for=email"])Output
["wrong-input-type"]