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)
    }
}
相关推荐
好开心33几秒前
axios的使用
开发语言·前端·javascript·前端框架·html
又蓝24 分钟前
使用 Python 操作 Excel 表格
开发语言·python·excel
余~~1853816280036 分钟前
稳定的碰一碰发视频、碰一碰矩阵源码技术开发,支持OEM
开发语言·人工智能·python·音视频
0zxm1 小时前
06 - Django 视图view
网络·后端·python·django
m0_748257181 小时前
Spring Boot FileUpLoad and Interceptor(文件上传和拦截器,Web入门知识)
前端·spring boot·后端
Am心若依旧4091 小时前
[c++11(二)]Lambda表达式和Function包装器及bind函数
开发语言·c++
明月看潮生1 小时前
青少年编程与数学 02-004 Go语言Web编程 20课题、单元测试
开发语言·青少年编程·单元测试·编程与数学·goweb
大G哥1 小时前
java提高正则处理效率
java·开发语言
VBA63372 小时前
VBA技术资料MF243:利用第三方软件复制PDF数据到EXCEL
开发语言
轩辰~2 小时前
网络协议入门
linux·服务器·开发语言·网络·arm开发·c++·网络协议