{"record":{"id":"81d407593c9dc661","repo":"kataras/iris","slug":"invalid-duration","errorCode":null,"errorMessage":"invalid duration","messagePattern":"invalid duration","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"x/jsonx/duration.go","lineNumber":35,"sourceCode":"\nfunc (d *Duration) UnmarshalJSON(b []byte) error {\n\tvar v any\n\tif err := json.Unmarshal(b, &v); err != nil {\n\t\treturn err\n\t}\n\tswitch value := v.(type) {\n\tcase float64:\n\t\t*d = Duration(value)\n\t\treturn nil\n\tcase string:\n\t\tv, err := time.ParseDuration(value)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\t*d = Duration(v)\n\t\treturn nil\n\tdefault:\n\t\treturn errors.New(\"invalid duration\")\n\t}\n}\n\nfunc (d Duration) ToDuration() time.Duration {\n\treturn time.Duration(d)\n}\n\nfunc (d Duration) Value() (driver.Value, error) {\n\treturn int64(d), nil\n}\n\n// Set sets the value of duration in nanoseconds.\nfunc (d *Duration) Set(v float64) {\n\tif math.IsNaN(v) {\n\t\treturn\n\t}\n\n\t*d = Duration(v)","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/kataras/iris/blob/7bedaf55a0b64bbb2248a5845a2c60d81a30996a/x/jsonx/duration.go#L17-L53","documentation":"jsonx.Duration.UnmarshalJSON returns 'invalid duration' when the JSON token cannot be interpreted as a duration number. The type accepts JSON numbers (and strings per the switch); any other JSON kind (bool, object, array, null) falls to the default branch and errors. It guards a custom time.Duration wrapper used in config/JSON decoding.","triggerScenarios":"Unmarshalling JSON where a Duration field receives a non-number, non-string token, e.g. {\"timeout\": true} or {\"timeout\": {}} instead of {\"timeout\": 5} or {\"timeout\": \"5s\"}.","commonSituations":"Typo or wrong unit in a config file that declares a duration field; API payload schema drift where a duration field became an object; forgetting that JSON has no native duration type.","solutions":["Supply the field as a number (nanoseconds) or a supported string form","Fix the JSON/config value's type to match the Duration field","Check with json.Valid or a schema validator before unmarshalling"],"exampleFix":"// before\n{\"timeout\": {\"seconds\": 5}}\n// after\n{\"timeout\": \"5s\"}","handlingStrategy":"validation","validationCode":"var probe map[string]json.RawMessage\nif err := json.Unmarshal(raw, &probe); err != nil { return err }\nif v, ok := probe[\"timeout\"]; ok && (v[0] == '{' || v[0] == '[' || v[0] == 't') {\n    return fmt.Errorf(\"timeout must be number or duration string\")\n}","typeGuard":"func isInvalidDuration(err error) bool { return err != nil && strings.Contains(err.Error(), \"invalid duration\") }","tryCatchPattern":"if err := json.Unmarshal(raw, &cfg); err != nil {\n    if strings.Contains(err.Error(), \"invalid duration\") {\n        return fmt.Errorf(\"config field %q: use e.g. \\\"5s\\\" or 5000000000\", \"timeout\")\n    }\n    return err\n}","preventionTips":["Document duration fields as numbers or Go duration strings","Schema-validate configs (JSON Schema with format: duration) before decode","Add a test fixture for every duration field"],"tags":["json","duration","unmarshal"],"backgroundTag":"json-unmarshal-type-mismatch","analyzedSha":"7bedaf55a0b64bbb2248a5845a2c60d81a30996a","analyzedAt":"2026-08-30T20:38:16.250Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}