forked from mtusi2026/mtusi2026lab3
refactor(app) agent refactoring + a little review
This commit is contained in:
@@ -22,71 +22,71 @@ func strLenBetween(s string, min, max int) bool {
|
||||
return n >= min && n <= max
|
||||
}
|
||||
|
||||
func ValidateCreateUser(username, displayName string) string {
|
||||
func ValidateCreateUser(username, displayName string) error {
|
||||
if !usernameRegex.MatchString(username) {
|
||||
return "username must match ^[a-z0-9_]{3,32}$"
|
||||
return BadRequest("username must match ^[a-z0-9_]{3,32}$")
|
||||
}
|
||||
|
||||
if blank(displayName) {
|
||||
return "display_name must not be empty or only whitespace"
|
||||
return BadRequest("display_name must not be empty or only whitespace")
|
||||
}
|
||||
|
||||
if !strLenBetween(displayName, 1, 100) {
|
||||
return "display_name must be between 1 and 100 Unicode code points"
|
||||
return BadRequest("display_name must be between 1 and 100 Unicode code points")
|
||||
}
|
||||
|
||||
return ""
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidatePassword(password string) string {
|
||||
func ValidatePassword(password string) error {
|
||||
if blank(password) {
|
||||
return "password must not be empty or only whitespace"
|
||||
return BadRequest("password must not be empty or only whitespace")
|
||||
}
|
||||
|
||||
if !strLenBetween(password, 12, 128) {
|
||||
return "password must be between 12 and 128 Unicode code points"
|
||||
return BadRequest("password must be between 12 and 128 Unicode code points")
|
||||
}
|
||||
|
||||
return ""
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateChatTitle(title string) string {
|
||||
func ValidateChatTitle(title string) error {
|
||||
if blank(title) {
|
||||
return "title must not be empty or only whitespace"
|
||||
return BadRequest("title must not be empty or only whitespace")
|
||||
}
|
||||
|
||||
if !strLenBetween(title, 1, 100) {
|
||||
return "title must be between 1 and 100 Unicode code points"
|
||||
return BadRequest("title must be between 1 and 100 Unicode code points")
|
||||
}
|
||||
|
||||
return ""
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateMessageText(text string) string {
|
||||
func ValidateMessageText(text string) error {
|
||||
if blank(text) {
|
||||
return "text must not be empty or only whitespace"
|
||||
return BadRequest("text must not be empty or only whitespace")
|
||||
}
|
||||
|
||||
if !strLenBetween(text, 1, 4000) {
|
||||
return "text must be between 1 and 4000 Unicode code points"
|
||||
return BadRequest("text must be between 1 and 4000 Unicode code points")
|
||||
}
|
||||
|
||||
return ""
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateIdempotencyKey(key string) string {
|
||||
func ValidateIdempotencyKey(key string) error {
|
||||
if key == "" {
|
||||
return "Idempotency-Key header is required"
|
||||
return BadRequest("Idempotency-Key header is required")
|
||||
}
|
||||
if !idempotencyKeyRegex.MatchString(key) {
|
||||
return "Idempotency-Key must match ^[A-Za-z0-9._:-]{1,128}$"
|
||||
return BadRequest("Idempotency-Key must match ^[A-Za-z0-9._:-]{1,128}$")
|
||||
}
|
||||
return ""
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateMemberIDs(ids []uuid.UUID, callerID uuid.UUID) string {
|
||||
func ValidateMemberIDs(ids []uuid.UUID, callerID uuid.UUID) error {
|
||||
if len(ids) < 1 || len(ids) > 100 {
|
||||
return "member_ids must contain between 1 and 100 members"
|
||||
return BadRequest("member_ids must contain between 1 and 100 members")
|
||||
}
|
||||
|
||||
seen := make(map[uuid.UUID]bool, len(ids))
|
||||
@@ -94,7 +94,7 @@ func ValidateMemberIDs(ids []uuid.UUID, callerID uuid.UUID) string {
|
||||
|
||||
for _, id := range ids {
|
||||
if seen[id] {
|
||||
return "member_ids must not contain duplicates"
|
||||
return BadRequest("member_ids must not contain duplicates")
|
||||
}
|
||||
|
||||
seen[id] = true
|
||||
@@ -105,8 +105,8 @@ func ValidateMemberIDs(ids []uuid.UUID, callerID uuid.UUID) string {
|
||||
}
|
||||
|
||||
if !callerFound {
|
||||
return "caller must be included in member_ids"
|
||||
return BadRequest("caller must be included in member_ids")
|
||||
}
|
||||
|
||||
return ""
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user