Problems › Python › validation
Which required fields are missing
A form submission arrives as a bag of strings and the server checks it before touching the database.
- A field is missing when its key is absent, or its value is empty or only whitespace.
- Report them in the order the requirements were given, not the order the payload happens to be in.
- Nothing required means nothing missing.
missing_fields(payload: map<string, string>, required: list<string>) → list<string>
Where you start
def missing_fields(payload: dict[str, str], required: list[str]) -> list[str]:
Worked examples
| Call | Result |
|---|---|
missing_fields({"name": "Ada", "email": ""}, ["name", "email", "phone"]) | ["email", "phone"] |
missing_fields({"name": " "}, ["name"]) | ["name"] |
missing_fields({"name": "Ada"}, ["name"]) | [] |
missing_fields({}, []) | [] |
Hint
Walk the requirements, not the payload — that is what fixes the output order.
Reference solution in Python
def missing_fields(payload: dict[str, str], required: list[str]) -> list[str]:
return [k for k in required if not payload.get(k, '').strip()]