{"record":{"id":"5f6df0c717112e1b","repo":"ipfs/kubo","slug":"type-field-missing-or-not-a-string","errorCode":null,"errorMessage":"'type' field missing or not a string","messagePattern":"'type' field missing or not a string","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"repo/fsrepo/datastores.go","lineNumber":81,"sourceCode":"\t}\n}\n\nfunc AddDatastoreConfigHandler(name string, dsc ConfigFromMap) error {\n\t_, ok := datastores[name]\n\tif ok {\n\t\treturn fmt.Errorf(\"already have a datastore named %q\", name)\n\t}\n\n\tdatastores[name] = dsc\n\treturn nil\n}\n\n// AnyDatastoreConfig returns a DatastoreConfig from a spec based on\n// the \"type\" parameter.\nfunc AnyDatastoreConfig(params map[string]any) (DatastoreConfig, error) {\n\twhich, ok := params[\"type\"].(string)\n\tif !ok {\n\t\treturn nil, fmt.Errorf(\"'type' field missing or not a string\")\n\t}\n\tfun, ok := datastores[which]\n\tif !ok {\n\t\treturn nil, fmt.Errorf(\"unknown datastore type: %s\", which)\n\t}\n\treturn fun(params)\n}\n\ntype mountDatastoreConfig struct {\n\tmounts []premount\n}\n\ntype premount struct {\n\tds     DatastoreConfig\n\tprefix ds.Key\n}\n\n// MountDatastoreConfig returns a mount DatastoreConfig from a spec.","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/ipfs/kubo/blob/329838acdfafae224582930457efe80aa217afc0/repo/fsrepo/datastores.go#L63-L99","documentation":"AnyDatastoreConfig builds a DatastoreConfig from a JSON-style spec map by first reading params[\"type\"]. If the key is absent or its value does not assert to string, the spec is unusable (the datastore registry is keyed by type name), so the function returns this error instead of guessing.","triggerScenarios":"Calling AnyDatastoreConfig(params) where params is a map[string]any parsed from a Datastore spec that lacks a \"type\" key, or where params[\"type\"] is a non-string value (e.g. int, nil, nested map from malformed JSON).","commonSituations":"Hand-edited or programmatically generated datastore spec in the repo config missing the \"type\" field; JSON unmarshalled into the wrong shape; a mount entry forgetting its type; config migration or templating tools dropping the field.","solutions":["Add a \"type\" key with the datastore name (e.g. \"mounts\", \"flatfs\", \"levelds\", \"measure\", \"log\") to the spec map","Print/inspect the spec map and fix JSON parsing so \"type\" survives as a string, e.g. use json.Unmarshal into map[string]any","If the spec came from a migration or tooling, regenerate it with a template that includes \"type\""],"exampleFix":"// before\nspec := map[string]any{\"path\": \"/datastore\"}\nds, err := fsrepo.AnyDatastoreConfig(spec) // error\n// after\nspec := map[string]any{\"type\": \"flatfs\", \"path\": \"/datastore\"}\nds, err := fsrepo.AnyDatastoreConfig(spec)","handlingStrategy":"validation","validationCode":"func validSpec(params map[string]any) bool {\n\tt, ok := params[\"type\"].(string)\n\treturn ok && t != \"\"\n}\nif !validSpec(spec) { /* fix spec before calling */ }","typeGuard":"func hasStringType(params map[string]any) (string, bool) {\n\tt, ok := params[\"type\"].(string)\n\treturn t, ok\n}","tryCatchPattern":"ds, err := fsrepo.AnyDatastoreConfig(spec)\nif err != nil {\n\tif strings.Contains(err.Error(), \"'type' field missing\") {\n\t\t// log spec keys and abort init\n\t}\n\treturn fmt.Errorf(\"invalid datastore spec %v: %w\", spec, err)\n}","preventionTips":["Always include a \"type\" string in every datastore spec map","Unmarshal config JSON with map[string]any so \"type\" arrives as a string","Validate specs against the registered datastore names before use"],"tags":["datastore","config","type-validation","go"],"backgroundTag":"datastore-spec-invalid","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"}