{"record":{"id":"ed6e5c41aba03a9b","repo":"grafana/k6","slug":"invalid-value-type","errorCode":null,"errorMessage":"invalid value type","messagePattern":"invalid value type","errorType":"validation","errorClass":"ErrInvalidValueType","httpStatus":null,"severity":"error","filePath":"metrics/value_type.go","lineNumber":13,"sourceCode":"package metrics\n\nimport \"errors\"\n\n// Possible values for ValueType.\nconst (\n\tDefault = ValueType(iota) // Values are presented as-is\n\tTime                      // Values are time durations (milliseconds)\n\tData                      // Values are data amounts (bytes)\n)\n\n// ErrInvalidValueType indicates the serialized value type is invalid.\nvar ErrInvalidValueType = errors.New(\"invalid value type\")\n\n// ValueType holds the type of values a metric contains.\ntype ValueType int\n\n// MarshalJSON serializes a ValueType to a JSON string.\nfunc (t ValueType) MarshalJSON() ([]byte, error) {\n\ttxt, err := t.MarshalText()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn []byte(`\"` + string(txt) + `\"`), nil\n}\n\n// MarshalText serializes a ValueType as a human readable string.\nfunc (t ValueType) MarshalText() ([]byte, error) {\n\tswitch t {\n\tcase Default:\n\t\treturn []byte(defaultString), nil","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/metrics/value_type.go#L1-L31","documentation":"ErrInvalidValueType (metrics/value_type.go:13) is returned by ValueType.UnmarshalText (line 51) when the input string is not 'default', 'time', or 'data', and by MarshalText (line 37) for an out-of-range ValueType integer. ValueType declares how metric values are rendered (as-is, milliseconds, bytes); it round-trips through JSON in metric definitions inside archives, the registry, and output payloads.","triggerScenarios":"json.Unmarshal of '\"duration\"', '\"ms\"', '\"bytes\"', or '\"DATA\"' (case-sensitive) into a metrics.ValueType field; marshaling ValueType(9). The only accepted strings are default, time, data.","commonSituations":"Exchanging metric metadata with tools that use different unit names ('bytes' vs 'data', 'ms' vs 'time'); hand-written JSON archives; case mismatches from template engines.","solutions":["Use exactly one of: default, time, data (lowercase)","Translate domain units to k6's names before deserializing (bytes -> data, ms -> time)","Validate external strings against the accepted set before unmarshaling"],"exampleFix":"// before\nvar vt metrics.ValueType\nerr := json.Unmarshal([]byte(`\"bytes\"`), &vt) // ErrInvalidValueType\n\n// after\nvar vt metrics.ValueType\nerr := json.Unmarshal([]byte(`\"data\"`), &vt)","handlingStrategy":"type-guard","validationCode":"// Go: whitelist check before unmarshaling\nvar validValueTypes = map[string]bool{\"default\": true, \"time\": true, \"data\": true}\nif !validValueTypes[input] {\n    return fmt.Errorf(\"unsupported value type %q; want default|time|data\", input)\n}","typeGuard":"// Go\nfunc isValidValueType(s string) bool {\n    switch s {\n    case \"default\", \"time\", \"data\":\n        return true\n    }\n    return false\n}","tryCatchPattern":"// Go\nif err := json.Unmarshal(data, &vt); err != nil {\n    if errors.Is(err, metrics.ErrInvalidValueType) {\n        // map unit names (bytes->data, ms->time) and retry, or reject the payload\n    }\n}","preventionTips":["k6 value types are exactly default, time, data — 'data' means bytes, 'time' means milliseconds","Translate external unit vocabularies before writing k6 metric JSON"],"tags":["metrics","serialization","json","validation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}