validation

package
v0.1.27 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateStruct

func ValidateStruct(v *Validator, s interface{})

ValidateStruct 验证结构体,根据字段标签进行验证

Types

type ValidationError

type ValidationError struct {
	Field   string // 字段名称
	Message string // 错误信息
}

ValidationError 表示验证错误

func (ValidationError) Error

func (e ValidationError) Error() string

type Validator

type Validator struct {
	Errors []ValidationError
}

Validator 提供数据验证功能

Example (Http)

示例:如何在HTTP处理程序中使用

// 这是一个示例HTTP处理程序
/*
	func RegisterHandler(w http.ResponseWriter, r *http.Request) {
		var user User

		// 解析请求JSON数据到结构体
		if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
			http.Error(w, "无效的请求数据", http.StatusBadRequest)
			return
		}

		// 验证用户数据
		v := validation.NewValidator()
		validation.ValidateStruct(v, &user)

		if !v.Valid() {
			// 返回验证错误
			w.Header().Set("Content-Type", "application/json")
			w.WriteHeader(http.StatusBadRequest)

			// 构建错误响应
			errorResponse := make(map[string]string)
			for _, err := range v.Errors {
				errorResponse[err.Field] = err.Message
			}

			json.NewEncoder(w).Encode(map[string]any{
				"errors": errorResponse,
			})
			return
		}

		// 继续处理有效请求...
	}
*/

func NewValidator

func NewValidator() *Validator

NewValidator 创建一个新的验证器

func (*Validator) AddError

func (v *Validator) AddError(field, message string)

AddError 添加一个验证错误

func (*Validator) Alpha

func (v *Validator) Alpha(value string, field string)

Alpha 验证字符串仅包含字母

func (*Validator) Alphanumeric

func (v *Validator) Alphanumeric(value string, field string)

Alphanumeric 验证字符串仅包含字母和数字

func (*Validator) Between

func (v *Validator) Between(value string, min, max int, field string)

Between 检查字符串长度是否在指定范围内

func (*Validator) Check

func (v *Validator) Check(ok bool, field, message string)

Check 检查条件是否成立,如果不成立则添加错误

func (*Validator) ChineseIDCard

func (v *Validator) ChineseIDCard(value string, field string)

ChineseIDCard 验证中国大陆身份证号

func (*Validator) Custom

func (v *Validator) Custom(ok bool, field, message string)

Custom 自定义验证

func (*Validator) Email

func (v *Validator) Email(value string, field string)

Email 验证邮箱格式

func (*Validator) InList

func (v *Validator) InList(value string, list []string, field string)

InList 验证值是否在列表中

func (*Validator) Integer

func (v *Validator) Integer(value string, field string)

Integer 验证字符串是否为整数

func (*Validator) MaxLength

func (v *Validator) MaxLength(value string, max int, field string)

MaxLength 检查字符串最大长度

func (*Validator) MinLength

func (v *Validator) MinLength(value string, min int, field string)

MinLength 检查字符串最小长度

func (*Validator) NotInList

func (v *Validator) NotInList(value string, list []string, field string)

NotInList 验证值是否不在列表中

func (*Validator) Numeric

func (v *Validator) Numeric(value string, field string)

Numeric 验证字符串是否为数字

func (*Validator) PhoneNumber

func (v *Validator) PhoneNumber(value string, field string)

PhoneNumber 验证手机号格式(中国大陆)

func (*Validator) Range

func (v *Validator) Range(value int, min, max int, field string)

Range 验证数字是否在指定范围内

func (*Validator) RangeFloat

func (v *Validator) RangeFloat(value float64, min, max float64, field string)

RangeFloat 验证浮点数是否在指定范围内

func (*Validator) Required

func (v *Validator) Required(value string, field string)

Required 检查字符串是否非空

func (*Validator) URL

func (v *Validator) URL(value string, field string)

URL 验证URL格式

func (*Validator) Valid

func (v *Validator) Valid() bool

Valid 检查验证器是否有错误

Jump to

Keyboard shortcuts

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