{"record":{"id":"bb02d30f9a426ced","repo":"go-playground/validator","slug":"err-error-from-strconv-parsebool","errorCode":null,"errorMessage":"err.Error() from strconv.ParseBool","messagePattern":"err\\.Error\\(\\) from strconv\\.ParseBool","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"util.go","lineNumber":317,"sourceCode":"// or panics if it can't convert\nfunc asFloat32(param string) float64 {\n\ti, err := strconv.ParseFloat(param, 32)\n\tpanicIf(err)\n\treturn i\n}\n\n// asBool returns the parameter as a bool\n// or panics if it can't convert\nfunc asBool(param string) bool {\n\ti, err := strconv.ParseBool(param)\n\tpanicIf(err)\n\n\treturn i\n}\n\nfunc panicIf(err error) {\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n}\n\n// Checks if field value matches regex. If fl.Field can be cast to Stringer, it uses the Stringer interfaces\n// String() return value. Otherwise, it uses fl.Field's String() value.\nfunc fieldMatchesRegexByStringerValOrString(regexFn func() *regexp.Regexp, fl FieldLevel) bool {\n\tregex := regexFn()\n\tswitch fl.Field().Kind() {\n\tcase reflect.String:\n\t\treturn regex.MatchString(fl.Field().String())\n\tdefault:\n\t\tif stringer, ok := getValue(fl.Field()).(fmt.Stringer); ok {\n\t\t\treturn regex.MatchString(stringer.String())\n\t\t} else {\n\t\t\treturn regex.MatchString(fl.Field().String())\n\t\t}\n\t}\n}","sourceCodeStart":299,"sourceCodeEnd":335,"githubUrl":"https://github.com/go-playground/validator/blob/facf128d2eecf42bd4ff447adc961aa21bb64aed/util.go#L299-L335","documentation":"util.go:317 panicIf converts any error returned by strconv.ParseInt/ParseUint/ParseFloat/ParseBool into a panic whose message is simply err.Error(). It backs the asInt/asUint/asFloat32/asFloat64/asBool helpers that evaluate numeric comparison parameters in tags like `min`, `max`, `gt`, `len`. A panic here means the numeric (or boolean) parameter inside a struct tag is malformed, e.g. `min=abc`.","triggerScenarios":"Using a numeric tag with a non-numeric or out-of-range parameter: `validate:\"min=10x\"`, `len=,` , `gtfield=` missing value, `validate:\"gtefield=NonNumericField\"` is fine but `max=1e999` (range) panics; a boolean-parse path triggered by a tag that expects true/false receiving garbage; dynamically generated tags with an empty or nil parameter string.","commonSituations":"Hand-written tags with typos in the numeric constant; tags built at runtime via fmt.Sprintf where a variable is empty or formatted wrong (e.g. locale decimal comma `min=1,5`); copying tags that used different parameter semantics; environment-driven configuration values interpolated into tags without sanitization.","solutions":["Find the struct tag with the failing parameter and correct the numeric/boolean literal (use '.' as the decimal separator).","When generating tags dynamically, validate/normalize parameters with strconv.ParseInt yourself before building the tag string.","Ensure numeric comparison tags are applied to fields whose tag expects a numeric param (min/max/len/gt/lt/gte/lte) and pass plain integers.","Add a unit test that constructs each dynamically-generated tag once at startup so the panic surfaces in tests, not production.","If parameters come from config/env, pre-validate them with a small parse step and fail fast with a clear message."],"exampleFix":"// before\nminParam := cfg.MinSize // \"1,5\"\ntag := fmt.Sprintf(\"gte=%s\", minParam)\n\n// after\nn, err := strconv.ParseFloat(strings.ReplaceAll(cfg.MinSize, \",\", \".\"), 64)\nif err != nil {\n    return fmt.Errorf(\"invalid min size %q\", cfg.MinSize)\n}\ntag := fmt.Sprintf(\"gte=%v\", n)","handlingStrategy":"validation","validationCode":"func validateTagParam(kind, p string) error {\n    var err error\n    switch kind {\n    case \"int\":\n        _, err = strconv.ParseInt(p, 10, 64)\n    case \"float\":\n        _, err = strconv.ParseFloat(p, 64)\n    case \"bool\":\n        _, err = strconv.ParseBool(p)\n    }\n    return err\n}","typeGuard":null,"tryCatchPattern":"func safeValidate(v *validator.Validate, s interface{}) (err error) {\n    defer func() {\n        if r := recover(); r != nil {\n            err = fmt.Errorf(\"malformed tag parameter: %v\", r)\n        }\n    }()\n    return v.Struct(s)\n}","preventionTips":["Never build tag strings from unsanitized env/config values; parse them first with strconv.","Always use '.' as decimal separator in tag parameters.","Test dynamically generated tags at startup.","Prefer setting numeric limits as Go constants in tags rather than interpolated strings."],"tags":["go","validator","strconv","panic","tag-parameter"],"backgroundTag":"invalid-numeric-tag-parameter","analyzedSha":"facf128d2eecf42bd4ff447adc961aa21bb64aed","analyzedAt":"2026-09-02T10:19:04.986Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}