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)
    }
}
相关推荐
亚图跨际1 分钟前
Python和R荧光分光光度法
开发语言·python·r语言·荧光分光光度法
Rverdoser9 分钟前
RabbitMQ的基本概念和入门
开发语言·后端·ruby
dj244294570712 分钟前
JAVA中的Lamda表达式
java·开发语言
流星白龙43 分钟前
【C++习题】10.反转字符串中的单词 lll
开发语言·c++
尘浮生1 小时前
Java项目实战II基于微信小程序的校运会管理系统(开发文档+数据库+源码)
java·开发语言·数据库·微信小程序·小程序·maven·intellij-idea
MessiGo1 小时前
Python 爬虫 (1)基础 | 基础操作
开发语言·python
Tech Synapse1 小时前
Java根据前端返回的字段名进行查询数据的方法
java·开发语言·后端
.生产的驴1 小时前
SpringCloud OpenFeign用户转发在请求头中添加用户信息 微服务内部调用
spring boot·后端·spring·spring cloud·微服务·架构
乌啼霜满天2491 小时前
JDBC编程---Java
java·开发语言·sql
微信-since811921 小时前
[ruby on rails] 安装docker
后端·docker·ruby on rails