validation

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultPasswordConfig = PasswordConfig{
	MinLength:      8,
	MaxLength:      72,
	RequireUpper:   true,
	RequireLower:   true,
	RequireDigit:   true,
	RequireSpecial: false,
}

DefaultPasswordConfig 默认密码配置。

P1 #17:MaxLength 由 128 收敛到 72(字节)。bcrypt 在 72 字节处静默截断—— 若允许更长密码,两个前 72 字节相同的不同密码会被视为同一密码通过认证。 length 用 len()(字节)度量,与 bcrypt 的字节截断口径一致。需支持超长密码时, 应在哈希前先做 SHA-256 预哈希再交给 bcrypt,而非放宽此上限。

Validator 全局验证器实例。

H-13 修复:改用 atomic.Pointer 保护读写,消除原裸指针(InitValidator 无锁写、 ValidateStruct 无锁读)在运行期热重载/并发校验时的数据竞争。类型由 *validator.Validate 变更为 atomic.Pointer[validator.Validate](breaking:下游若直接读 validation.Validator.Struct 需改用 ValidateStruct,或 validation.Validator.Load().Struct)。

Functions

func CheckPassword

func CheckPassword(hashedPassword, password string) bool

CheckPassword 校验 hashedPassword 是否匹配明文密码。

func CheckPasswordAndUpgrade

func CheckPasswordAndUpgrade(hashedPassword, password string, targetCost int) (match bool, needUpgrade bool, newHash string, err error)

CheckPasswordAndUpgrade 校验密码,并在存量 hash 的 cost 低于归一化后的目标 cost 时重新哈希。

func GetPasswordCost

func GetPasswordCost(hashedPassword string) (int, error)

GetPasswordCost 返回 hashedPassword 中编码的 bcrypt cost。

func HashPassword

func HashPassword(password string) (string, error)

HashPassword 使用框架默认 cost 哈希明文密码。

func HashPasswordWithCost

func HashPasswordWithCost(password string, cost int) (string, error)

HashPasswordWithCost 使用指定 cost 哈希明文密码。

低于 bcrypt.MinCost 的 cost 会提升到 bcrypt.MinCost,高于 bcrypt.MaxCost 的 cost 会降到 bcrypt.MaxCost。

func InitValidator

func InitValidator()

InitValidator 初始化验证器

func ValidatePassword

func ValidatePassword(password string) (bool, string)

ValidatePassword 验证密码强度 返回:是否有效,错误信息

func ValidatePasswordWithConfig

func ValidatePasswordWithConfig(password string, config PasswordConfig) (bool, string)

ValidatePasswordWithConfig 使用指定配置验证密码强度

Types

type PasswordConfig

type PasswordConfig struct {
	MinLength      int  // 最小长度
	MaxLength      int  // 最大长度
	RequireUpper   bool // 需要大写字母
	RequireLower   bool // 需要小写字母
	RequireDigit   bool // 需要数字
	RequireSpecial bool // 需要特殊字符
}

PasswordConfig 密码验证配置

type ValidationError

type ValidationError struct {
	Field   string `json:"field"`   // 字段名(使用 label 或 json tag)
	Label   string `json:"label"`   // 字段中文名(用于显示)
	Message string `json:"message"` // 错误消息
}

ValidationError 验证错误

type ValidationErrors

type ValidationErrors []ValidationError

ValidationErrors 验证错误列表

func BindAndValidate

func BindAndValidate(c *gin.Context, req any) ValidationErrors

BindAndValidate 绑定并验证请求

func BindForm

func BindForm(c *gin.Context, req any) ValidationErrors

BindForm 绑定 Form 并验证

func BindJSON

func BindJSON(c *gin.Context, req any) ValidationErrors

BindJSON 绑定 JSON 并验证

func BindQuery

func BindQuery(c *gin.Context, req any) ValidationErrors

BindQuery 绑定 Query 并验证

func ShouldBindAndValidate

func ShouldBindAndValidate(c *gin.Context, req any) (ValidationErrors, bool)

ShouldBindAndValidate 绑定并验证请求,返回是否成功

func ValidateStruct

func ValidateStruct(s any) ValidationErrors

ValidateStruct 验证结构体

func (ValidationErrors) Error

func (ve ValidationErrors) Error() string

Error 实现 error 接口

func (ValidationErrors) First

func (ve ValidationErrors) First() *ValidationError

First 获取第一个错误

func (ValidationErrors) FirstMessage

func (ve ValidationErrors) FirstMessage() string

FirstMessage 获取第一个错误消息

func (ValidationErrors) ToLabelMap

func (ve ValidationErrors) ToLabelMap() map[string]string

ToLabelMap 转换为带标签的 map

func (ValidationErrors) ToMap

func (ve ValidationErrors) ToMap() map[string]string

ToMap 转换为 map

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL