Files
usernode/internal/pkg/validate.go
T
cao.wangrenbo ae45aba607 feat(M0): 工程骨架 — Go 后端 + Vue3 前端脚手架
- Go 工程:cmd/usernode CLI(serve/migrate/admin create/reset-password/user otp)
  + internal/{config,model,service,system,auth,cron,api,router,server,pkg,webui}
- 配置:TOML + 环境变量覆盖(USERNODE_<SEC>_<FIELD>),config.example.toml
- 数据:GORM 双驱动(SQLite/MySQL)8 表模型,migrate 子命令可跑通
- 系统层:system.Manager 接口(useradd/userdel/passwd 白名单,dev dry-run)
- HTTP:Gin 路由骨架(/api/v1 + 501 占位),healthz,slog 结构化日志,优雅启停
- 前端:Vite + Vue3 + TS + Element Plus 最小可运行(web/),go:embed 打通
- 构建:deploy/Containerfile 多阶段单二进制镜像,web/Containerfile.dev 前端 dev
  镜像,Makefile(build/test/dev/web-build/image);go.mod 锁定 go 1.22
- 验证:go build/vet/test 通过;podman 镜像构建运行 healthz+embed 通过
2026-08-29 22:43:27 +08:00

43 lines
1.3 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 pkg
import (
"fmt"
"regexp"
)
// 外部用户系统账号约束:ext_ 前缀(可配置)+ 小写字母开头的 2~31 位 [a-z0-9]
// 总长不超过 32(Linux 系统账号名上限)。
var (
reUserName = regexp.MustCompile(`^[a-z][a-z0-9]{1,31}$`)
reEmail = regexp.MustCompile(`^[^@\s]+@[^@\s]+\.[^@\s]+$`)
)
// ValidateUserName 校验申请/创建时使用的用户名(不含 ext_ 前缀)。
func ValidateUserName(name string) error {
if !reUserName.MatchString(name) {
return fmt.Errorf("用户名只能由小写字母与数字组成,以字母开头,长度 2~32(不含前缀)")
}
return nil
}
// ValidateSystemAccount 校验最终系统账号(含前缀)合法性。
func ValidateSystemAccount(account string) error {
if len(account) > 32 || len(account) == 0 {
return fmt.Errorf("系统账号长度必须在 1~32 之间")
}
for _, r := range account {
if !(r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '_' || r == '-') {
return fmt.Errorf("系统账号只能包含小写字母、数字、下划线和连字符")
}
}
return nil
}
// ValidateEmail 做基础格式校验。
func ValidateEmail(email string) error {
if len(email) > 255 || !reEmail.MatchString(email) {
return fmt.Errorf("邮箱格式不合法")
}
return nil
}