validate

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2021 License: MIT Imports: 5 Imported by: 0

README

简要说明

// r 为 *http.Request
// 结构体增加标签 validate(验证规则) 和 fieldName(显示的字段别名)如下u为结构体的实例
bool,string = validate.StructValidate(r, u)

// map数据验证 map[字段名][]string{"规则1","规则2"...} , 别名(选传)map[字段名]别名
bool,string = validate.MapValidate(r, mapData,fieldMap)

完整示例

import (
 "github.com/wuyan94zl/gowebpkg/validate"
)

type User struct {
    Id   uint32 `validate:"numeric"fieldName:"ID"`
    Name string `validate:"required||min:6||max:32"fieldName:"用户名称"`
}
u := User{Id: 2, Name: "无言"}
r := &http.Request{}
if ok, msg := validate.StructValidate(r, u); !ok {
    fmt.Println(msg)
}
mapData := make(map[string][]string)
mapData["Id"] = []string{"numeric"}
mapData["Name"] = []string{"required","min:10","max:32"}
mapMsg := make(map[string]string)
mapMsg["Id"] = "ID"
mapMsg["Name"] = "会员名称"
// mapMsg 可以不传如: validate.MapValidate(r, mapData)
if ok, msg := validate.MapValidate(r, mapData,mapMsg); !ok {
    fmt.Println(msg)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapDataForStruct

func MapDataForStruct(data interface{}) (map[string][]string, map[string]string)

func MapValidate

func MapValidate(r *http.Request, data map[string][]string, validateMsg ...map[string]string) (bool, string)

func StructValidate

func StructValidate(r *http.Request, data interface{}) (bool, string)

Types

This section is empty.

Jump to

Keyboard shortcuts

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