当前位置:网站首页>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 .
边栏推荐
- Easyanticheat uses to inject unsigned code into a protected process (2)
- Jimureport building block report - expression introduction
- Abnova多肽设计和合成解决方案
- Ribbon
- Leetcode refers to offer II 089 House theft
- How to build a website for ECS? What are the prices of different ECS
- 让孩子们学习Steam 教育的应用精髓
- Idea creates a servlet and accesses the 404 message
- 解析90后创客教育的主观积极性
- ribbon
猜你喜欢
uni-app进阶之认证【day12】

Abnova peptide design and synthesis solutions

How does the compiler put the first instruction executed by the chip at the start address of the chip?

Introduction to C language custom types (structure, enumeration, union, bit segment)

『渗透基础』Cobalt Strike基础使用入门_Cobalt Strike联动msfconsole

编译器是如何将芯片执行的第一个指令放到芯片起始地址的?

『应急响应实践』LogParser日志分析实践

解析90后创客教育的主观积极性

What is etcd and its application scenarios

Congratulations to Zhong Jun for becoming the maintainer of chaos metric model working group
随机推荐
lldb常用命令
SAP mts/ato/mto/eto topic 8: ATO mode 2 d+ empty mode strategy 85
Multi task video recommendation scheme, baidu engineers' actual combat experience sharing
Real time monitoring: system and application level real-time monitoring based on flow computing Oceanus (Flink)
Weibo International Edition changed its name to Weibo light sharing Edition
SAP mts/ato/mto/eto topic 10: ETO mode q+ empty mode unvalued inventory policy customization
由浅入深的混合精度训练教程
How does ECS select bandwidth? What types of servers do you usually have?
Advantages of fixed assets management system
Replication of variables in golang concurrency
Abnova多肽设计和合成解决方案
Network timeout configuration method when PR and push are proposed
Web penetration test - 5. Brute force cracking vulnerability - (6) VNC password cracking
web渗透测试----5、暴力破解漏洞--(6)VNC密码破解
oracle数据库提示无操作权限的问题
Abnova fluorescence in situ hybridization (FISH) probe solution
DP summary of ACM in recent two weeks
图书馆管理后台
What is the principle of Ping? How does the server disable Ping?
What are the functions of ASP files on ECs? What if the ECS cannot run ASP with a low version?