{"record":{"id":"9f56e9f1a1e0e160","repo":"go-playground/validator","slug":"bad-param-number-for-required-if-s","errorCode":null,"errorMessage":"Bad param number for required_if %s","messagePattern":"Bad param number for required_if (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"baked_in.go","lineNumber":2123,"sourceCode":"\n\tcase reflect.Ptr:\n\t\tif field.IsNil() {\n\t\t\treturn value == \"nil\"\n\t\t}\n\t\t// Handle non-nil pointers\n\t\treturn requireCheckFieldValue(fl, param, value, defaultNotFoundValue)\n\t}\n\n\t// default reflect.String:\n\treturn field.String() == value\n}\n\n// requiredIf is the validation function\n// The field under validation must be present and not empty only if all the other specified fields are equal to the value following with the specified field.\nfunc requiredIf(fl FieldLevel) bool {\n\tparams := parseOneOfParam2(fl.Param())\n\tif len(params)%2 != 0 {\n\t\tpanic(fmt.Sprintf(\"Bad param number for required_if %s\", fl.FieldName()))\n\t}\n\n\tseen := make(map[string]struct{})\n\tfor i := 0; i < len(params); i += 2 {\n\t\tif _, ok := seen[params[i]]; ok {\n\t\t\tpanic(fmt.Sprintf(\"Duplicate param %s for required_if %s\", params[i], fl.FieldName()))\n\t\t}\n\t\tseen[params[i]] = struct{}{}\n\t}\n\n\tfor i := 0; i < len(params); i += 2 {\n\t\tif !requireCheckFieldValue(fl, params[i], params[i+1], false) {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn hasValue(fl)\n}\n","sourceCodeStart":2105,"sourceCodeEnd":2141,"githubUrl":"https://github.com/go-playground/validator/blob/facf128d2eecf42bd4ff447adc961aa21bb64aed/baked_in.go#L2105-L2141","documentation":"required_if requires an even number of comma-separated parameters: field,value pairs (each 'other field must equal value'). parseOneOfParam2 splits fl.Param(); if the count is odd, a pair is incomplete and the validator panics with 'Bad param number for required_if'.","triggerScenarios":"Tag like `required_if=FieldA` (one param) or `required_if=FieldA,1,FieldB` (three params) — any odd param count after parsing. Note params containing commas are split by parseOneOfParam2, so an unescaped comma inside a value can also shift the count to odd.","commonSituations":"Hand-written struct tags forgetting the comparison value; dynamically generated tags joining an odd number of tokens; values containing commas not understood by the param parser; typo'd tag copied from required_without (which takes a plain field list).","solutions":["Supply field,value pairs: change `required_if=OtherField` to `required_if=OtherField,someValue`.","Count params after comma-splitting; escape or remove stray commas inside values.","If you only need 'other field present', use the correct tag (e.g. required_with) instead of required_if.","Add a unit test calling Validate.Struct on the struct so tag syntax errors surface at test time."],"exampleFix":"// before\nActive bool\nEmail string `validate:\"required_if=Active\"`\n\n// after\nActive bool\nEmail string `validate:\"required_if=Active,true\"`","handlingStrategy":"validation","validationCode":"func checkRequiredIfTag(tagValue string) error {\n    parts := strings.Split(tagValue, \",\")\n    if len(parts)%2 != 0 {\n        return fmt.Errorf(\"required_if needs field,value pairs; got %d params in %q\", len(parts), tagValue)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"func safeValidate(v *Validate, s interface{}) (err error) {\n    defer func() {\n        if r := recover(); r != nil {\n            err = fmt.Errorf(\"validator panic (check required_if params): %v\", r)\n        }\n    }()\n    return v.Struct(s)\n}","preventionTips":["Always write required_if as field,value pairs: required_if=Field,value","Lint struct tags in CI (e.g. go vet fieldalignment-style custom check or a tag linter)","Escape/remove commas inside comparison values","Cover each conditional tag with a Validate.Struct test"],"tags":["go","validator","panic","struct-tags","required-if"],"backgroundTag":"invalid-validator-tag-params","analyzedSha":"facf128d2eecf42bd4ff447adc961aa21bb64aed","analyzedAt":"2026-09-02T10:19:04.986Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}