在GO语言中进行验证,使用”ozzo-validation”库。

huangapple go评论67阅读模式
英文:

Validation in GO "ozzo-validation"

问题

我是你的中文翻译助手,以下是翻译好的内容:

我不是GO的高手,只是尝试使用gin和名为ozzo-validation的插件创建一个简单的crud

我的代码:

package models

import (
	validation "github.com/go-ozzo/ozzo-validation"
	"gorm.io/gorm"
)

type Post struct {
	gorm.Model
	Name string `gorm:"type:varchar(255);"`
	Desc string `gorm:"type:varchar(500);"`
}

type PostData struct {
	Name string `json:"name"`
	Desc string `json:"desc"`
}

func (p PostData) Validate() error {
	return validation.ValidateStruct(&p,
		validation.Field(&p.Name, validation.Required, validation.Length(5, 20)),
		validation.Field(&p.Desc, validation.Required),
	)
}

PostController:

package controllers

import (
	"curd/app/models"
	"fmt"
	"github.com/gin-gonic/gin"
)

func Store(c *gin.Context) {
	// 验证输入
	var post models.PostData
	err := post.Validate()
	fmt.Println(err)
}
{
  "name": "sdfsdfsfsdf"
}

问题是,当我从Postman提交上述JSON数据时,验证器在终端中给出以下错误信息:

desc: 不能为空;name: 不能为空。
英文:

I'm nob in GO 在GO语言中进行验证,使用”ozzo-validation”库。 just try to create simple crud throw it using gin and plugin called ozzo-validation

My code:

package models

import (
	validation "github.com/go-ozzo/ozzo-validation"
	"gorm.io/gorm"
)

type Post struct {
	gorm.Model
	Name string `gorm:"type:varchar(255);"`
	Desc string `gorm:"type:varchar(500);"`
}

type PostData struct {
	Name string `json:"name"`
	Desc string `json:"desc"`
}

func (p PostData) Validate() error {
	return validation.ValidateStruct(&p,
		validation.Field(&p.Name, validation.Required, validation.Length(5, 20)),
		validation.Field(&p.Desc, validation.Required),
	)
}

PostController:

package controllers

import (
	"curd/app/models"
	"fmt"
	"github.com/gin-gonic/gin"
)

func Store(c *gin.Context) {
	// Validate Input
	var post models.PostData
	err := post.Validate()
	fmt.Println(err)
}
{
  "name": "sdfsdfsfsdf"
}

The problem is once I submit the above JSON data from the postman the validation gives me this in terminal :

desc: cannot be blank; name: cannot be blank.

答案1

得分: 0

如评论中所述,您需要在执行验证之前将HTTP请求中的数据解码为结构体。您看到的验证错误是在您的Post结构体的新实例(每个字段都具有零值)上调用Validate()的结果。请尝试这个

func Store(c *gin.Context) {
	var post models.PostData
	// 这将根据内容类型标头推断要使用的绑定器。
	if err := c.ShouldBind(&post); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
    // 验证输入
    err := post.Validate()
    fmt.Println(err)
}
英文:

As noted in the comments, you need to decode the data from the HTTP request into the struct before you can perform validation. The validation errors you're seeing are a product of calling Validate() on a fresh instance (with zero values in every field) of your Post struct. Try this.

func Store(c *gin.Context) {
	var post models.PostData
	// This will infer what binder to use depending on the content-type header.
	if err := c.ShouldBind(&post); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
    // Validate Input
    err := post.Validate()
    fmt.Println(err)
}

huangapple
  • 本文由 发表于 2021年7月25日 22:57:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/68519770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定