{"record":{"id":"ad5906438fff148b","repo":"vxcontrol/pentagi","slug":"invalid-userstatus-s","errorCode":null,"errorMessage":"invalid UserStatus: %s","messagePattern":"invalid UserStatus: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/pkg/server/models/users.go","lineNumber":32,"sourceCode":"type UserStatus string\n\nconst (\n\tUserStatusCreated UserStatus = \"created\"\n\tUserStatusActive  UserStatus = \"active\"\n\tUserStatusBlocked UserStatus = \"blocked\"\n)\n\nfunc (s UserStatus) String() string {\n\treturn string(s)\n}\n\n// Valid is function to control input/output data\nfunc (s UserStatus) Valid() error {\n\tswitch s {\n\tcase UserStatusCreated, UserStatusActive, UserStatusBlocked:\n\t\treturn nil\n\tdefault:\n\t\treturn fmt.Errorf(\"invalid UserStatus: %s\", s)\n\t}\n}\n\n// Validate is function to use callback to control input/output data\nfunc (s UserStatus) Validate(db *gorm.DB) {\n\tif err := s.Valid(); err != nil {\n\t\tdb.AddError(err)\n\t}\n}\n\ntype UserType string\n\nconst (\n\tUserTypeLocal UserType = \"local\"\n\tUserTypeOAuth UserType = \"oauth\"\n\tUserTypeAPI   UserType = \"api\"\n)\n","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/vxcontrol/pentagi/blob/ea665308baaff015b226f308438a68d929d0f29b/backend/pkg/server/models/users.go#L14-L50","documentation":"UserStatus.Valid() is a GORM model validation guard for the users table in backend/pkg/server/models/users.go:32. It only accepts the enum values UserStatusCreated, UserStatusActive, and UserStatusBlocked; any other string carried in a User's Status field is rejected with this formatted error. It runs via the Validate(db) callback whenever a User record is created or updated, so a bad status blocks persistence instead of writing corrupt data.","triggerScenarios":"Saving (Create/Save/Updates) or serializing a User whose Status field is an empty string, a hand-written string like \"actve\", or any value not exactly equal to one of the three accepted enum constants.","commonSituations":"Constructing a User struct in code or tests without initializing Status; inserting rows via raw SQL or an external service that writes status values not present in the Go enum set; refactoring or DB migration that renames status strings without updating the constants.","solutions":["Set the user's Status to one of the exported constants (UserStatusCreated, UserStatusActive, UserStatusBlocked) before saving.","If the value comes from external input, map/normalize it to a known constant and reject unknown values at the API boundary.","If a new status is genuinely required, add a UserStatus* constant and extend the switch in Valid() rather than passing a raw string."],"exampleFix":"// before\nuser := models.User{Name: \"alice\"} // Status empty\norm.Create(&user) // invalid UserStatus: \n\n// after\nuser := models.User{Name: \"alice\", Status: models.UserStatusActive}\norm.Create(&user)","handlingStrategy":"validation","validationCode":"if err := models.UserStatus(status).Valid(); err != nil {\n    return fmt.Errorf(\"invalid status %q: %w\", status, err)\n}","typeGuard":"func validUserStatus(s models.UserStatus) bool {\n    switch s {\n    case models.UserStatusCreated, models.UserStatusActive, models.UserStatusBlocked:\n        return true\n    }\n    return false\n}","tryCatchPattern":"if err := orm.Create(&user).Error; err != nil {\n    if strings.HasPrefix(err.Error(), \"invalid UserStatus\") {\n        return fmt.Errorf(\"bad status %q: %w\", user.Status, err)\n    }\n    return err\n}","preventionTips":["Only assign Status via the exported constants, never raw strings.","Validate user-controlled status values at the API boundary before building the model.","Add a unit test asserting every persisted status passes Valid()."],"tags":["go","validation","enum","gorm"],"backgroundTag":"invalid-enum-value","analyzedSha":"ea665308baaff015b226f308438a68d929d0f29b","analyzedAt":"2026-09-01T14:16:31.421Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}