go-playground/validator · error

Bad field name %s

Error message

Bad field name %s

What it means

Panics inside the isUnique validator's slice-of-struct branch when the parameter naming the key field does not exist on the struct elements. After resolving each element to a struct, FieldByName(param) returns an invalid value because the tag parameter names a field that is not present on that struct; the guard panics to signal a misconfigured unique=FieldName tag rather than silently passing.

Source

Thrown at baked_in.go:427

			if elem.Kind() == reflect.Ptr {
				if elem.IsNil() {
					if _, ok := seen[nilKey]; ok {
						return false
					}
					seen[nilKey] = struct{}{}
					continue
				}
				elem = elem.Elem()
			}

			if elem.Kind() != reflect.Struct {
				panic(fmt.Sprintf("Bad field type %s", elem.Type()))
			}

			sf := elem.FieldByName(param)
			if !sf.IsValid() {
				panic(fmt.Sprintf("Bad field name %s", param))
			}

			var key interface{}

			if sf.Kind() == reflect.Ptr {
				if sf.IsNil() {
					key = nilKey
				} else {
					key = sf.Elem().Interface()
				}
			} else {
				key = sf.Interface()
			}

			if _, ok := seen[key]; ok {
				return false
			}
			seen[key] = struct{}{}

View on GitHub (pinned to facf128d2e)

Solutions

  1. Correct the unique tag parameter to match an actual field name on the struct (case-sensitive)
  2. Add the missing key field to the struct if it was intended
  3. Keep the field name in sync when renaming struct fields used by unique tags
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at baked_in.go:427 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of go-playground/validator@facf128d2e (2026-09-02). Data as JSON: /api/errors/0de22ccce116f3de. Report an issue: GitHub.