Drill

ProblemsGo › validation

Which required fields are missing

mediumvalidationHash mapsArraysGo

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>

Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

func missingFields(payload map[string]string, required []string) []string {
	
}

Worked examples

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

Hint

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

Reference solution in Go
func missingFields(payload map[string]string, required []string) []string {
	result := []string{}
	for _, key := range required {
		if strings.TrimSpace(payload[key]) == "" {
			result = append(result, key)
		}
	}
	return result
}

The same problem in another language

More validation problems in Go