validation

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

DefaultPasswordConfig 默认密码配置

View Source
var Validator *validator.Validate

Validator 全局验证器实例

Functions

func CheckPassword

func CheckPassword(hashedPassword, password string) bool

CheckPassword 验证密码是否匹配 hashedPassword 是加密后的密码,password 是明文密码

func CheckPasswordAndUpgrade

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

CheckPasswordAndUpgrade 验证密码并在需要时升级加密成本 返回:是否匹配、是否需要升级、升级后的密码、错误

func GetPasswordCost

func GetPasswordCost(hashedPassword string) (int, error)

GetPasswordCost 获取加密密码的成本

func HashPassword

func HashPassword(password string) (string, error)

HashPassword 对密码进行加密

func HashPasswordWithCost

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

HashPasswordWithCost 使用指定成本对密码进行加密 cost 范围: 4-31,值越大越安全但越慢

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