{"record":{"id":"53de81dee5ebf117","repo":"gofiber/fiber","slug":"datetime-constraint-requires-a-layout-argument","errorCode":null,"errorMessage":"datetime constraint requires a layout argument","messagePattern":"datetime constraint requires a layout argument","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"constraint.go","lineNumber":314,"sourceCode":"\t}\n\treturn true\n}\n\ntype guidConstraintType struct{}\n\nfunc (guidConstraintType) Name() string { return ConstraintGUID }\nfunc (guidConstraintType) Execute(param string, _ []any) bool {\n\t_, err := uuid.Parse(param)\n\treturn err == nil\n}\n\ntype datetimeConstraintType struct{}\n\nfunc (datetimeConstraintType) Name() string { return ConstraintDatetime }\nfunc (datetimeConstraintType) Analyze(args []string) ([]any, error) {\n\targs = parseConstraintArgs(args)\n\tif len(args) == 0 {\n\t\treturn nil, errors.New(\"datetime constraint requires a layout argument\")\n\t}\n\treturn []any{args[0]}, nil\n}\n\nfunc (datetimeConstraintType) Execute(param string, data []any) bool {\n\tif len(data) == 0 {\n\t\treturn false\n\t}\n\tlayout, ok := data[0].(string)\n\tif !ok || layout == \"\" {\n\t\treturn false\n\t}\n\t_, err := time.Parse(layout, param)\n\treturn err == nil\n}\n\ntype minLenConstraintType struct{}\n","sourceCodeStart":296,"sourceCodeEnd":332,"githubUrl":"https://github.com/gofiber/fiber/blob/a105acad6c1e4576a77f01e02973f67e962bb58d/constraint.go#L296-L332","documentation":"Returned by datetimeConstraintType.Analyze (constraint.go:313-315) when a route parameter uses the datetime constraint but no layout argument was supplied. Analyze is called from newConstraint during route registration (constraint.go:187-191); a returned error simply leaves c.typedData empty, and Execute then returns false for every value — but Analyze's error is the canonical signal that the route declaration is malformed.","triggerScenarios":"Registering a route like app.Get(\"/:when<datetime>\", ...) without the required layout argument. The datetime constraint needs the layout form, e.g. \"/:when<datetime(2006-01-02)>\"; omitting it leaves the constraint with no way to parse the parameter.","commonSituations":"Copy/paste from a route that used minLen (which requires a number) into datetime without updating the argument; reading docs that omitted the argument; refactoring a route and dropping the parenthesized argument.","solutions":["Add a Go time layout argument to the constraint: \"/:when<datetime(2006-01-02)>\".","Use a layout constant and interpolate it consistently across routes that share a format.","If the parameter is free-form, drop the constraint entirely or switch to regex.","Add a route-registration test that exercises each constrained path with a valid sample."],"exampleFix":"// before\napp.Get(\"/:when<datetime>\", handler) // missing layout\n\n// after\napp.Get(\"/:when<datetime(2006-01-02)>\", handler)","handlingStrategy":"validation","validationCode":"// Validate the constraint declaration at route-registration time\nfunc checkDatetimeLayout(arg string) error {\n    if arg == \"\" { return errors.New(\"datetime constraint requires a layout argument\") }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"// Route registration either succeeds or fails fast at startup; treat\n// the Analyze error as a build-time signal by exercising the route in a test.\nfunc TestRoutesRegister(t *testing.T) {\n    app := fiber.New()\n    register(app) // your route registration\n    // If a datetime constraint lacks its layout, the route simply won't match;\n    // assert expected matches/non-matches here.\n}","preventionTips":["Always include a Go time layout in datetime constraints: <datetime(2006-01-02)>.","Write a registration test that hits each constrained route with a valid sample.","Centralize route declarations and review constraint arguments in code review."],"tags":["routing","constraints","datetime","fiber"],"backgroundTag":null,"analyzedSha":"a105acad6c1e4576a77f01e02973f67e962bb58d","analyzedAt":"2026-08-11T17:33:26.942Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}