当前位置:网站首页>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 .
边栏推荐
- Network timeout configuration method when PR and push are proposed
- SAP mts/ato/mto/eto topic 7: ATO mode 1 m+m mode strategy 82 (6892)
- Openeuler kernel technology sharing issue 20 - execution entity creation and switching
- ServiceStack. Source code analysis of redis (connection and connection pool)
- Replication of variables in golang concurrency
- Specificity and correlation of Worthington deoxyribonuclease I
- What is Ping? How can the server disable Ping?
- [2021 "shadow seeking" medical artificial intelligence algorithm competition] frequently asked questions related to Ti-One products
- ribbon
- What technology is VPS? How does the server VPS?
猜你喜欢

Clang code coverage detection (pile insertion technology)

SAP mts/ato/mto/eto topic 8: ATO mode 2 d+ empty mode strategy 85

Abnova membrane protein lipoprotein solution
2020年Android面试题汇总(中级)

15+ urban road element segmentation application, this segmentation model is enough

一款支持内网脱机分享文档的接口测试软件

大一下学期期末总结(补充知识漏洞)
![Web technology sharing | [map] to realize customized track playback](/img/b2/25677ca08d1fb83290dd825a242f06.png)
Web technology sharing | [map] to realize customized track playback

MySQL - SQL execution process

解析90后创客教育的主观积极性
随机推荐
少儿编程课程改革后的培养方式
IP and traffic reconciliation tool networktrafficview
Ribbon
数据库解答建标,按要求回答
web渗透测试----5、暴力破解漏洞--(5)SMB密码破解
一款支持内网脱机分享文档的接口测试软件
黑马程序员机器学习讲义:线性回归api初步使用
MySQL - SQL execution process
How to create an FTP server on the ECS? Is it safe to create an FTP server on the ECS?
Abnova peptide design and synthesis solutions
web渗透测试----5、暴力破解漏洞--(8)PostgreSQL密码破解
SAP MTS/ATO/MTO/ETO专题之七:ATO模式1 M+M模式策略用82(6892)
How to enlarge the ECS page? How to select ECS instance specifications?
SAP mts/ato/mto/eto topic 10: ETO mode q+ empty mode unvalued inventory policy customization
Replication of variables in golang concurrency
How does the VPS server upload data? Is the VPS server free to use?
What are the functions of ASP files on ECs? What if the ECS cannot run ASP with a low version?
Introduction to C language custom types (structure, enumeration, union, bit segment)
Library management backstage
What are the advantages of ECS? Is ECS better than VM?