Drill

ProblemsTypeScript › validation

Which required fields are missing

mediumvalidationTypeScript

A form submission arrives as a bag of strings and the server checks it before touching the database.

missingFields(payload: map<string, string>, required: list<string>) → list<string>

Solve it in the editor →

Where you start

function missingFields(payload: Record<string, string>, required: string[]): string[] {
  
}

Worked examples

CallResult
missingFields({"name":"Ada","email":""}, ["name","email","phone"])["email","phone"]
missingFields({"name":" "}, ["name"])["name"]
missingFields({"name":"Ada"}, ["name"])[]
missingFields({}, [])[]

Hint

Walk the requirements, not the payload — that is what fixes the output order.

Reference solution in TypeScript
function missingFields(payload: Record<string, string>, required: string[]): string[] {
  const result: string[] = [];
  for (const key of required) {
    const v = payload[key];
    if (v === undefined || v.trim() === '') result.push(key);
  }
  return result;
}

The same problem in another language

More validation problems in TypeScript