当前位置:网站首页>Verifying data models in golang

Verifying data models in golang

2022-06-24 04:44:00 jerryteng

When we deal with our business logic ,

Generally, the information transmitted from the front end , The back-end code must hold An attitude of distrust of all information ,

All input information needs to be verified ,

We are in the process of code practice , With xml Written idl Check the data , The calibration tool is go-playground Of validator

What is? validator Well ?

stay web Data validation problems are often encountered in applications , Common verification methods are cumbersome , Here is a package that uses more validator

It has the following unique features:

  • Cross Field and Cross Struct validations by using validation tags or custom validators.
  • Slice, Array and Map diving, which allows any or all levels of a multidimensional field to be validated.
  • Ability to dive into both map keys and values for validation
  • Handles type interface by determining it's underlying type prior to validation.
  • Handles custom field types such as sql driver Valuer see Valuer

...

Here we will not expand in detail ,

Here is an example :

package main

import (
    "fmt"
    "github.com/go-playground/validator/v10"
)

type Users struct {
    Phone   string `form:"phone" json:"phone" validate:"required"`
    Passwd   string `form:"passwd" json:"passwd" validate:"required,max=20,min=6"`
}

func main() {

    users := &Users{
        Phone:      "1326654487",
        Passwd:       "123",
    }
    validate := validator.New()
    err := validate.Struct(users)
    if err != nil {
        for _, err := range err.(validator.ValidationErrors) {
            fmt.Println(err)//Key: 'Users.Passwd' Error:Field validation for 'Passwd' failed on the 'min' tag
            return
        }
    }
    return
}

It's easy to use .

Today, I also encountered a character encoding problem , The code is as follows :

type UpdateRequest struct {
	// id
	Id int64 `json:"Id"  form:"Id"  binding:"required,gte=1,lte=9223372036854775807"`
	//  Media name 
	Name string `json:"Name"  form:"Name"  validate:"required,omitempty,gte=1,lte=8"

In the database , Defined Name The length is 32, Check in the code name The length is 8, Now I want to let go 8 The Chinese characters ,

because In the database , Defined Name The length is 32 , therefore , This doesn't need to be adjusted , This is from MySQL The nature of ,

To adjust the code :

type UpdateRequest struct {
	// id
	Id int64 `json:"Id"  form:"Id"  binding:"required,gte=1,lte=9223372036854775807"`
	//  Media name 
	Name string `json:"Name"  form:"Name"  validate:"required,omitempty,gte=1,lte=32"
	

I found that I could pass , Finally, check the source code

original validator When performing string verification in , It's using tune , A Chinese character , It's also a character , Here is what we need to pay attention to ,

however validator Still very human , We just need this kind of verification .

原网站

版权声明
本文为[jerryteng]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/09/20210906203045899Z.html