Fix the new user validator for name and age

Review the AI's codeEasy10 min

Your task

A pytest for your signup check wrapped the call in try with except Exception: pass, so it passed whether the check raised or not. The team now has the check return a reason code, so a test can assert which rule rejected the user.

An assistant wrote validate_new_user(name, age). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • name: the name as typed. The only whitespace characters it can contain are spaces, tabs, newlines and carriage returns.
  • age: the age, an integer. It can be negative or very large.
  • Return one of "name_required", "age_negative", "age_too_large" or "ok".

Rules the tests check:

  • Check these rules in this order, and return the code of the first one that applies:
    1. The name is blank: empty, or only whitespace. Return "name_required".
    2. The age is below 0. Return "age_negative".
    3. The age is above 130. Return "age_too_large".
  • If none applies, return "ok". Ages 0 and 130 are both valid.
  • A name with whitespace around real characters, such as " bob ", is a valid name.

Examples

validate_new_user(name: str, age: int) -> str
Example 1
Inputvalidate_new_user("x", -5)
Output"age_negative"
Example 2
Inputvalidate_new_user("x", 30)
Output"ok"
Example 3
Inputvalidate_new_user("", 30)
Output"name_required"