golang validator基于map规则验证集合和结构体

validator基于map规则验证集合和结构体

validator可以基于map规则进行集合的校验以及结构体的校验,同时支持嵌套校验

主要函数

  • validate.ValidateMap(map, rules)

    验证集合

  • validate.RegisterStructValidationMapRules(structType, Data{})

    验证结构体

集合验证
go 复制代码
package validate

import (
    "fmt"
    "log"
)

// 集合验证
func ValidateMap() {
    user := map[string]interface{} {
        "name": "hdddccccc",
        "emain": "hddd@google.com",
    }

    rules := map[string]interface{} {
        "name": "required,min=8,max=15",
        "email": "omitempty,email",
    }

    err := validate.ValidateMap(user, rules)
    if err != nil {
        fmt.Println(err)
    }

}

// 集合嵌套验证
func ValidateNestedMap() {
    data := map[string]interface{} {
        "name": "ddkalsj",
        "email": "djsta@as.com",
        "details": map[string]interface{}{
            "contact_address": map[string]interface{}{
                "province": "湖南",
                "city":     "长沙",
            },
            "age": 18,
            "phones": []map[string]interface{}{
                {
                    "number": "11-111-1111",
                    "remark": "home",
                },
                {
                    "number": "22-222-2222",
                    "remark": "work",
                },
            },
        },
    }

    rules := map[string]interface{}{
        "name":  "min=4,max=15",
        "email": "required,email",
        "details": map[string]interface{}{
            "contact_address": map[string]interface{}{
                "province": "required",
                "city":     "required",
            },
            "age": "numeric,min=18",
            "phones": map[string]interface{}{
                "number": "required,min=4,max=32",
                "remark": "required,min=1,max=32",
            },
        },
    }

    err := validate.ValidateMap(data, rules)
    if err != nil {
        log.Fatal(err)
    }
}
结构体验证
go 复制代码
package validate

import "fmt"

// map规则于结构体中的应用
type Data struct {
    Name    string
    Email   string
    Details *Details
}
type Details struct {
    ContactAddress *ContactAddress
    Age            uint8
    Phones         []*Phone
}
type ContactAddress struct {
    Province string
    City     string
}
type Phone struct {
    Number string
    Remark string
}

func ValidateStructNested() {
    data := Data{
        Name:  "kangkang",
        Email: "kangkang@edgg.com",
        Details: &Details{
            ContactAddress: &ContactAddress{
                Province: "四川",
                City:     "成都",
            },
            Age: 18,
            Phones: []*Phone{
                {
                    Number: "11-111-1111",
                    Remark: "home",
                },
                {
                    Number: "22-2111-1111",
                    Remark: "work",
                },
            },
        },
    }
    // map 字段验证规则
    dataRules := map[string]string{
        "Name":    "min=4,max=15",
        "Email":   "required,email",
        "Details": "required",
    }

    detailRules := map[string]string{
        "Age":            "number,min=18,max=130",
        "ContactAddress": "required",
        "Phones":         "required,min=1,dive",
    }
    contactAddressRules := map[string]string{
        "Province": "required",
        "City":     "required",
    }
    phoneRules := map[string]string{
        "Number": "required,min=4,max=32",
        "Remark": "required,min=1,max=32",
    }
	// 注册对应结构体验证
    validate.RegisterStructValidationMapRules(dataRules, Data{})
    validate.RegisterStructValidationMapRules(detailRules, Details{})
    validate.RegisterStructValidationMapRules(contactAddressRules, ContactAddress{})
    validate.RegisterStructValidationMapRules(phoneRules, Phone{})
    err := validate.Struct(data)
    if err != nil {
        fmt.Println(err)
    }
}
相关推荐
爱找乐子的李寻欢12 分钟前
线上批量导出 1000 个文件触发 OOM?扒开代码看本质,我是这样根治的
后端
q***016517 分钟前
Python爬虫完整代码拿走不谢
开发语言·爬虫·python
顺心而行...18 分钟前
一些问题记录
开发语言
u***j32423 分钟前
JavaScript在Node.js中的进程管理
开发语言·javascript·node.js
未来之窗软件服务43 分钟前
幽冥大陆(三十五)S18酒店门锁SDK go语言——东方仙盟筑基期
java·前端·golang·智能门锁·仙盟创梦ide·东方仙盟·东方仙盟sdk
大鸡腿同学1 小时前
大量频繁记录有效击球方式
后端
沐知全栈开发1 小时前
前端控制器模式
开发语言
稚辉君1 小时前
Gemini永久会员 01不等概率随机到01等概率随机
后端
0***86331 小时前
【Golang】——Gin 框架中的表单处理与数据绑定
microsoft·golang·gin
C***11501 小时前
对基因列表中批量的基因进行GO和KEGG注释
开发语言·数据库·golang