{"record":{"id":"04ed0e31f07e3c59","repo":"JuliusBrussee/caveman","slug":"tenantkey-safe-month-is-required","errorCode":null,"errorMessage":"tenantkey: safe month is required","messagePattern":"tenantkey: safe month is required","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/tenantkey/tenantkey.go","lineNumber":26,"sourceCode":")\n\n// CavePlan returns worker/gateway shared published-plan key.\nfunc CavePlan(organizationID, projectID string) (string, error) {\n\tprefix, err := projectPrefix(\"plan\", organizationID, projectID)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\treturn prefix, nil\n}\n\n// MonthlySpend returns gateway/control-api shared monthly spend key.\nfunc MonthlySpend(organizationID, projectID, month string) (string, error) {\n\tprefix, err := projectPrefix(\"spend\", organizationID, projectID)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tif !safePart(month) {\n\t\treturn \"\", errors.New(\"tenantkey: safe month is required\")\n\t}\n\treturn prefix + \":\" + month, nil\n}\n\n// MonthlySpendEvent returns idempotency-marker key for one spend event.\nfunc MonthlySpendEvent(organizationID, projectID, month, requestID string) (string, error) {\n\tprefix, err := projectPrefix(\"spend-event\", organizationID, projectID)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tif !safePart(month) || !safePart(requestID) {\n\t\treturn \"\", errors.New(\"tenantkey: safe month and request id are required\")\n\t}\n\treturn prefix + \":\" + month + \":\" + requestID, nil\n}\n\nfunc projectPrefix(namespace, organizationID, projectID string) (string, error) {\n\tif !safePart(organizationID) || !safePart(projectID) {","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/tenantkey/tenantkey.go#L8-L44","documentation":"MonthlySpend builds a tenant-scoped spend key and requires the month component to pass safePart (a non-empty, characters-safe segment). If the month is empty or contains unsafe characters (which could alter key structure), the library returns this error rather than producing a malformed or colliding key.","triggerScenarios":"Calling tenantkey.MonthlySpend(orgID, projectID, month) with month = \"\" or a month containing unsafe characters (e.g. \"2026/09\", \"2026-09 extra\", values with ':' or whitespace) instead of a clean token like \"2026-09\".","commonSituations":"A missing month parameter from a report/CLI command; a date formatted with a slash (\"2026/09\") or locale text (\"September 2026\"); a time.Time formatted with the wrong layout; upstream data with untrimmed whitespace.","solutions":["Format the month canonically before calling: t.Format(\"2006-01\") produces \"2026-09\", which is safePart-safe.","Validate/normalize the month input (strings.TrimSpace, reject empties) at the caller boundary and return a descriptive error to the user.","If the month comes from user input or a path parameter, whitelist-regex it (e.g. ^\\d{4}-\\d{2}$) before invoking MonthlySpend."],"exampleFix":"// before\nkey, err := tenantkey.MonthlySpend(org, proj, req.URL.Query().Get(\"month\")) // may be \"2026/09\" or \"\"\n// after\nmonth := strings.TrimSpace(req.URL.Query().Get(\"month\"))\nif !regexp.MustCompile(`^\\d{4}-\\d{2}$`).MatchString(month) {\n    return errors.New(\"month must be in YYYY-MM format\")\n}\nkey, err := tenantkey.MonthlySpend(org, proj, month)","handlingStrategy":"validation","validationCode":"var monthRe = regexp.MustCompile(`^\\d{4}-\\d{2}$`)\nfunc validMonth(m string) bool { return monthRe.MatchString(strings.TrimSpace(m)) }","typeGuard":null,"tryCatchPattern":"key, err := tenantkey.MonthlySpend(org, proj, month)\nif err != nil && strings.Contains(err.Error(), \"safe month is required\") {\n    return fmt.Errorf(\"month %q must be a non-empty YYYY-MM token\", month)\n}","preventionTips":["Always produce month values with time.Format(\"2006-01\")","Reject empty/unsafe segments at the request boundary with a clear user-facing error","Add a unit test that feeds malformed months (\"\", \"2026/09\") to catch regressions"],"tags":["tenantkey","validation","key-format","go"],"backgroundTag":"missing-required-argument","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}