1 Star 0 Fork 0

也许/gin

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Struct level validations

Validations can also be registered at the struct level when field level validations don't make much sense. This can also be used to solve cross-field validation elegantly. Additionally, it can be combined with tag validations. Struct Level validations run after the structs tag validations.

Example requests

# Validation errors are generated for struct tags as well as at the struct level
$ curl -s -X POST http://localhost:8085/user \
	-H 'content-type: application/json' \
	-d '{}' | jq
{
  "error": "Key: 'User.Email' Error:Field validation for 'Email' failed on the 'required' tag\nKey: 'User.FirstName' Error:Field validation for 'FirstName' failed on the 'fnameorlname' tag\nKey: 'User.LastName' Error:Field validation for 'LastName' failed on the 'fnameorlname' tag",
  "message": "User validation failed!"
}

# Validation fails at the struct level because neither first name nor last name are present
$ curl -s -X POST http://localhost:8085/user \
    -H 'content-type: application/json' \
	-d '{"email": "george@vandaley.com"}' | jq
{
  "error": "Key: 'User.FirstName' Error:Field validation for 'FirstName' failed on the 'fnameorlname' tag\nKey: 'User.LastName' Error:Field validation for 'LastName' failed on the 'fnameorlname' tag",
  "message": "User validation failed!"
}

# No validation errors when either first name or last name is present
$ curl -X POST http://localhost:8085/user \
    -H 'content-type: application/json' \
	-d '{"fname": "George", "email": "george@vandaley.com"}'
{"message":"User validation successful."}

$ curl -X POST http://localhost:8085/user \
    -H 'content-type: application/json' \
	-d '{"lname": "Contanza", "email": "george@vandaley.com"}'
{"message":"User validation successful."}

$ curl -X POST http://localhost:8085/user \
    -H 'content-type: application/json' \
	-d '{"fname": "George", "lname": "Costanza", "email": "george@vandaley.com"}'
{"message":"User validation successful."}

Useful links

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Happay-Code/gin.git
git@gitee.com:Happay-Code/gin.git
Happay-Code
gin
gin
v1.3.0

搜索帮助