{"record":{"id":"dac933e546460d00","repo":"ipfs/kubo","slug":"invalid-flag-value-d","errorCode":null,"errorMessage":"invalid flag value: %d","messagePattern":"invalid flag value: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"config/types.go","lineNumber":89,"sourceCode":"\tcase Default:\n\t\treturn defaultValue\n\tcase True:\n\t\treturn true\n\tdefault:\n\t\tpanic(fmt.Sprintf(\"invalid flag value %d\", f))\n\t}\n}\n\nfunc (f Flag) MarshalJSON() ([]byte, error) {\n\tswitch f {\n\tcase Default:\n\t\treturn json.Marshal(nil)\n\tcase True:\n\t\treturn json.Marshal(true)\n\tcase False:\n\t\treturn json.Marshal(false)\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"invalid flag value: %d\", f)\n\t}\n}\n\nfunc (f *Flag) UnmarshalJSON(input []byte) error {\n\tswitch string(input) {\n\tcase \"null\":\n\t\t*f = Default\n\tcase \"false\":\n\t\t*f = False\n\tcase \"true\":\n\t\t*f = True\n\tdefault:\n\t\treturn fmt.Errorf(\"failed to unmarshal %q into a flag: must be null/undefined, true, or false\", string(input))\n\t}\n\treturn nil\n}\n\nfunc (f Flag) String() string {","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/ipfs/kubo/blob/329838acdfafae224582930457efe80aa217afc0/config/types.go#L71-L107","documentation":"Flag.MarshalJSON in config/types.go marshals the tri-state Flag (Default/True/False) to JSON. If the Flag holds an out-of-range integer value that is none of the three defined states, marshaling fails with \"invalid flag value: %d\". This indicates the Flag was constructed programmatically with an invalid value, since JSON unmarshaling cannot produce one.","triggerScenarios":"Building a kubo Config struct in Go (e.g. a library consumer or test) and assigning an arbitrary integer to a config.Flag field, then serializing it with json.Marshal or writing it via WriteConfigFile/serialize.","commonSituations":"Custom node-construction code (kubo-as-a-library) setting Flag fields with raw integers instead of config.Default/config.True/config.False; fuzzing or reflection-based config generation producing bogus values.","solutions":["Only assign the exported constants config.Default, config.True, or config.False to Flag fields.","Zero-initialize Flags (config.Default is the zero state that marshals to null).","If the value comes from user input, parse it with Flag.UnmarshalJSON (accepting null/true/false) instead of casting."],"exampleFix":"// before\ncfg.Provider.Enabled = config.Flag(7)\n// after\ncfg.Provider.Enabled = config.True","handlingStrategy":"type-guard","validationCode":"func validFlag(f config.Flag) bool { return f == config.Default || f == config.True || f == config.False }","typeGuard":"func isValidFlag(f config.Flag) bool {\n    switch f {\n    case config.Default, config.True, config.False:\n        return true\n    }\n    return false\n}","tryCatchPattern":"if err := json.Marshal(cfg); err != nil {\n    var fe *json.MarshalerError\n    if errors.As(err, &fe) && strings.Contains(fe.Error(), \"invalid flag value\") { /* fix Flag field */ }\n    return err\n}","preventionTips":["Only assign config.Default/True/False constants to Flag fields","Parse user input with Flag.UnmarshalJSON instead of integer casts","Add unit tests that marshal any programmatically built config"],"tags":["config","json","go","marshaling"],"backgroundTag":"invalid-enum-value","analyzedSha":"329838acdfafae224582930457efe80aa217afc0","analyzedAt":"2026-09-03T18:30:52.135Z","contentChangedAt":"2026-09-03T18:30:52.135Z","schemaVersion":2},"datasetVersion":"2026-09-11T00:17:11.886Z"}