Validate panics with "Bad field type" when an `any` field holds a non-string value
Validate panics with Bad field type when an any field holds a non-string, while a null in the same field is correctly reported
The string-oriented built-in validators end with panic(fmt.Sprintf("Bad field type %s", field.Type())) in the default branch of their switch field.Kind(). On a typed field that is a defensible fail-fast. But on a field declared any the kind is the dynamic kind of whatever the value holds, so the same struct and tag validate cleanly for one value and panic for another.
The two failure modes are already inconsistent with each other, which is what makes this look like a defect rather than a policy. A null in the field is handled properly:
type Profile struct {
Website any `json:"website" validate:"url"`
Locale any `json:"locale" validate:"bcp47_language_tag"`
Zone any `json:"zone" validate:"timezone"`
}// all three null -> rejected, one fieldError each:
// Key: 'Profile.Website' Error:Field validation for 'Website' failed on the 'url' tag ...
// all three valid strings -> valid, no error
{"website":12345,...} -> panic: Bad field type float64
{"locale":true,...} -> panic: Bad field type bool
{"zone":{"tz":"UTC"},...} -> panic: Bad field type map[string]interface {}encoding/json puts a float64 in an any field for a JSON number, so the only thing deciding whether this is a validation error or a crash is whether a client sent a number where a string was expected. The panic escapes Validate, so there is no error to hand back.
Why the kind is data-dependent
traverseField unwraps the field before running the tag — current, kind, v.fldIsPointer = v.extractTypeInternal(current, false) at validator.go:94 — so the validator sees the dynamic kind and panics in its default branch (isISO3166Alpha2, baked_in.go:1473). any fields with validate tags are a supported and tested pattern here: validator_test.go has MyInt interface{} \validate:"omitempty,gte=2,lte=255"``.
Scope
Reading the 209 keys of bakedInValidators out of baked_in.go and exercising each bare tag on an any field holding "US", float64(12345), true, []any{}: 37 need a parameter and cannot be exercised bare; of the remaining 172, 35 panic with Bad field type for at least one value. 33 of those accept "US" cleanly, so the verdict really is decided by the value. (port and iso4217_numeric panic on every value; latitude/longitude accept a float64 but panic on bool.)
bcp47_language_tag, bcp47_strict_language_tag, country_code, datetime, dir, dirpath, eq_ignore_case, eu_country_code, file, filepath, http_url, https_url, image, iso3166_1_alpha_numeric, iso3166_1_alpha_numeric_eu, json, latitude, longitude, lowercase, luhn_checksum, mimetype, ne_ignore_case, noneof, noneofci, oneof, oneofci, origin, timezone, uppercase, uri, url, urn_rfc2141, urn_rfc8141
Before changing anything
No patch from me yet: the fix is a semantics decision, and the module path is /v10. Three answers look reasonable —
- Report an error when the value cannot satisfy the tag. A field whose static kind is
interface{}holding a non-string simply failsurl, so afieldErrorfits what thenullcase already does. Keep the panic where the schema really is wrong. - Keep the panic, make it actionable — put the field path and tag in the message.
- Leave it, document it — string tags must not go on
anyfields.
I lean to 1, but 35 call sites and v10 compatibility are your call. Happy to send a PR for whichever you prefer, with tests.
Checked so as not to duplicate: #1201/#1596 are about runValidationWhenNil not being honoured per tag, so a nil pointer runs a comparison validator; #1147 is Bad field type v1.Duration, where the static type really does not match the tag. Neither covers an any field holding a non-nil non-string.
Reproduced on master at 3487bab with Go 1.26.0.
AI-assisted development. Human review requested before merge. The probe builds one struct type per tag with reflect.StructOf, recovers panics, and separates Bad field type from the Undefined validation function panic an unknown tag produces. The tag list is parsed out of baked_in.go, not hand-written, since a hand-written list turns an invented tag into a false positive.
Source: go-playground/validator