forked from mtusi2026/mtusi2026lab2
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package production
|
|
|
|
import (
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab2/internal/service"
|
|
)
|
|
|
|
// These bounds come from the mandatory lab1 OpenAPI schema.
|
|
const (
|
|
minUsernameLength = 3
|
|
maxUsernameLength = 32
|
|
maxDisplayNameRunes = 100
|
|
maxChatTitleRunes = 100
|
|
maxChatMembers = 100
|
|
maxMessageRunes = 4000
|
|
canonicalUUIDLength = len("00000000-0000-0000-0000-000000000000")
|
|
)
|
|
|
|
// validText checks that text is nonblank and within its Unicode length limit.
|
|
func validText(value string, maxRunes int) bool {
|
|
return utf8.ValidString(value) && strings.TrimSpace(value) != "" && utf8.RuneCountInString(value) <= maxRunes
|
|
}
|
|
|
|
// validUsername checks the username length and permitted ASCII characters.
|
|
func validUsername(value string) bool {
|
|
if len(value) < minUsernameLength || len(value) > maxUsernameLength {
|
|
return false
|
|
}
|
|
for _, char := range value {
|
|
if (char < 'a' || char > 'z') && (char < '0' || char > '9') && char != '_' {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// parseID parses a canonical UUID or returns a validation error.
|
|
func parseID(value string) (uuid.UUID, error) {
|
|
if len(value) != canonicalUUIDLength {
|
|
return uuid.Nil, service.ErrInvalid
|
|
}
|
|
id, err := uuid.Parse(value)
|
|
if err != nil {
|
|
return uuid.Nil, service.ErrInvalid
|
|
}
|
|
return id, nil
|
|
}
|