Files
mtusi2026lab6/internal/service/validate.go
T

54 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package service
import (
"regexp"
"strings"
"unicode/utf8"
)
var usernameRe = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
// validatePassword: 12–128 code points и не только пробельные символы
// (обязательная строка по контракту не может состоять из одних пробелов).
func validatePassword(s string) bool {
if strings.TrimSpace(s) == "" {
return false
}
n := utf8.RuneCountInString(s)
return n >= 12 && n <= 128
}
// validateUsername проверяет формат username по README: строго [a-z0-9_]{3,32}.
func validateUsername(s string) bool {
return usernameRe.MatchString(s)
}
// validateDisplayName проверяет display_name: 1–100 Unicode code points,
// не состоит только из пробельных символов.
func validateDisplayName(s string) bool {
if strings.TrimSpace(s) == "" {
return false
}
n := utf8.RuneCountInString(s)
return n >= 1 && n <= 100
}
// validateTitle проверяет title чата: 1–100 code points, не только пробелы.
func validateTitle(s string) bool {
if strings.TrimSpace(s) == "" {
return false
}
n := utf8.RuneCountInString(s)
return n >= 1 && n <= 100
}
// validateText проверяет text сообщения: 1–4000 code points, не только пробелы.
// В lab4 появится возможность отправить пустой text, если есть attachment_ids.
func validateText(s string) bool {
if strings.TrimSpace(s) == "" {
return false
}
n := utf8.RuneCountInString(s)
return n >= 1 && n <= 4000
}