
Validate the Recruits
New apprentices sign up through a form that saves nested JSON. Some entries are… creative. Your job: check each record and explain exactly what's wrong, so a human can fix it.
A good validator never crashes on bad data. Use dict.get() with a
default to walk nested structures safely:
rec = {"name": "Ada"}
rec.get("skills", {}).get("python") # None, no KeyError
Assignment
validate(rec) returns a list of error strings, in this order,
for each rule that fails (an empty list means the record is valid):
"name: missing":nameis absent or blank after stripping."email: invalid":emailmust fully matchEMAIL_RE."joined: invalid":joinedmust parse as%Y-%m-%d."level: invalid":rec["skills"]["python"]must be anintfrom 1 to 5 (it may be missing at any depth).
Then problems(json_text) loads {"recruits": [...]} and returns a
dict mapping each invalid record's position (0-based index) to its
error list.
validate(rec) returns a list of error strings, in this order,
for each rule that fails (an empty list means the record is valid):
"name: missing":nameis absent or blank after stripping."email: invalid":emailmust fully matchEMAIL_RE."joined: invalid":joinedmust parse as%Y-%m-%d."level: invalid":rec["skills"]["python"]must be anintfrom 1 to 5 (it may be missing at any depth).
Then problems(json_text) loads {"recruits": [...]} and returns a
dict mapping each invalid record's position (0-based index) to its
error list.
Press Run to try your code, then Check to test it.