• 参数校验go-playground/validator


    package main
    
    import (
    	"fmt"
    	"github.com/go-playground/validator/v10"
    )
    
    // User contains user information
    type User struct {
    	FirstName      string     `validate:"required"`
    	LastName       string     `validate:"required"`
    	Age            uint8      `validate:"gte=0,lte=130"`
    	Email          string     `validate:"required,email"`
    	FavouriteColor string     `validate:"iscolor"`                // alias for 'hexcolor|rgb|rgba|hsl|hsla'
    	Addresses      []*Address `validate:"required,dive,required"` // a person can have a home and cottage...
    }
    
    // Address houses a users address information
    type Address struct {
    	Street string `validate:"required"`
    	City   string `validate:"required"`
    	Planet string `validate:"required"`
    	Phone  string `validate:"required"`
    }
    
    // use a single instance of Validate, it caches struct info
    var validate *validator.Validate
    
    func main() {
    
    	validate = validator.New()
    
    	validateStruct()
    	validateVariable()
    }
    
    func validateStruct() {
    
    	address := &Address{
    		Street: "Eavesdown Docks",
    		Planet: "Persphone",
    		Phone:  "none",
    	}
    
    	user := &User{
    		FirstName:      "Badger",
    		LastName:       "Smith",
    		Age:            135,
    		Email:          "Badger.Smith@gmail.com",
    		FavouriteColor: "#000-",
    		Addresses:      []*Address{address},
    	}
    
    	// returns nil or ValidationErrors ( []FieldError )
    	err := validate.Struct(user)
    	if err != nil {
    
    		// this check is only needed when your code could produce
    		// an invalid value for validation such as interface with nil
    		// value most including myself do not usually have code like this.
    		if _, ok := err.(*validator.InvalidValidationError); ok {
    			fmt.Println(err)
    			return
    		}
    
    		for _, err := range err.(validator.ValidationErrors) {
    
    			fmt.Println(err.Namespace())
    			fmt.Println(err.Field())
    			fmt.Println(err.StructNamespace())
    			fmt.Println(err.StructField())
    			fmt.Println(err.Tag())
    			fmt.Println(err.ActualTag())
    			fmt.Println(err.Kind())
    			fmt.Println(err.Type())
    			fmt.Println(err.Value())
    			fmt.Println(err.Param())
    			fmt.Println()
    		}
    
    		// from here you can create your own error messages in whatever language you wish
    		return
    	}
    
    	// save user to database
    }
    
    func validateVariable() {
    
    	myEmail := "joeybloggs.gmail.com"
    
    	errs := validate.Var(myEmail, "required,email")
    
    	if errs != nil {
    		fmt.Println(errs) // output: Key: "" Error:Field validation for "" failed on the "email" tag
    		return
    	}
    
    	// email ok, move on
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
  • 相关阅读:
    pdf转换器是干什么用的,各种文档互转离不开它!
    SIM卡相关知识介绍
    Linux系统自动化安装
    [模块化编写驱动控制LED灯]
    ROS1云课→17化繁为简stdr和f1tenth
    【Gradle】四、使用Gradle创建SringBoot微服务项目
    Python | 打包 .py 为可执行文件.exe
    Vue47-修改默认配置webpack.config.js文件
    DIY单片机STC51控制海尔热水器,带电量计量,走时DS1302,温度DS18B20带CRC,程序全公开
    “智能+”时代,深维智信如何借助阿里云打造AI内容生成系统
  • 原文地址:https://blog.csdn.net/dawnto/article/details/128046033