gofiber: 用go-playground/validator校验参数,自定义错误信息

一,go-playground/validator官方代码地址

https://github.com/go-playground/validator

二,安装

$ go get -u github.com/go-playground/validator/v10
go: downloading github.com/go-playground/validator/v10 v10.23.0
go: downloading github.com/gabriel-vasile/mimetype v1.4.3
go: downloading github.com/go-playground/universal-translator v0.18.1
go: downloading github.com/leodido/go-urn v1.4.0
go: downloading golang.org/x/crypto v0.28.0
go: downloading github.com/gabriel-vasile/mimetype v1.4.7
go: downloading golang.org/x/crypto v0.29.0
go: downloading github.com/go-playground/locales v0.14.1
go: downloading golang.org/x/net v0.30.0
go: downloading golang.org/x/net v0.31.0
go: added github.com/gabriel-vasile/mimetype v1.4.7
go: added github.com/go-playground/locales v0.14.1
go: added github.com/go-playground/universal-translator v0.18.1
go: added github.com/go-playground/validator/v10 v10.23.0
go: added github.com/leodido/go-urn v1.4.0
go: upgraded golang.org/x/crypto v0.28.0 => v0.29.0
go: upgraded golang.org/x/net v0.30.0 => v0.31.0

 三,应用例子:

1,代码

package controller

import (
	"bytes"
	"fmt"
	"github.com/go-playground/validator/v10"
	"github.com/gofiber/fiber/v2"
	"industry/service"
	"runtime"
	"strconv"
)

type ArticleController struct{}

func NewArticleController() *ArticleController {
	return &ArticleController{}
}


var Validator = validator.New()

type User struct {
    Cid  int    `validate:"required"`
    Bid  int    `validate:"required"`
}

func (dc *ArticleController) ListArticle(c *fiber.Ctx) error {
	var user User
    //也可以用c.BodyParser解析post数据
	if err := c.QueryParser(&user); err != nil {
		fmt.Print(err)
		printStackTrace(err)
	}

	if validationErr := Validator.Struct(&user); validationErr != nil {
		return c.SendString("参数解析:"+validationErr.Error())
	}
    //其他逻辑
}

 

测试效果:

参数解析:Key: 'User.Cid' Error:Field validation for 'Cid' failed on the 'required' tag

四,自定义报错信息

1,结构体:

type User struct {
    Cid  int    `validate:"required" label:"cid需要为整数,请重新输入"`
    Bid  int    `validate:"required" label:"bid需要为整数,请重新输入"`
}

2,返回中文报错信息的函数:

// ProcessErr go validator参数校验器自定义规则及提示
func ValidateMessage(u interface{}, err error) string {
	if err == nil { //如果为nil 说明校验通过
		return ""
	}
	invalid, ok := err.(*validator.InvalidValidationError) //如果是输入参数无效,则直接返回输入参数错误
	if ok {
		return "输入参数错误:" + invalid.Error()
	}
	validationErrs := err.(validator.ValidationErrors) //断言是ValidationErrors
	for _, validationErr := range validationErrs {
		fieldName := validationErr.Field() //获取是哪个字段不符合格式
		typeOf := reflect.TypeOf(u)
		// 如果是指针,获取其属性
		if typeOf.Kind() == reflect.Ptr {
			typeOf = typeOf.Elem()
		}
		field, ok := typeOf.FieldByName(fieldName) //通过反射获取filed
		if ok {
			errorInfo := field.Tag.Get("label") // 获取field对应的reg_error_info tag值
			return fieldName + ":" + errorInfo           // 返回错误
		} else {
			return fieldName + ":" + validationErr.Error()
		}
	}
	return ""
}

说明:label字段不存在时,按默认提示信息返回

3,调用代码

func (dc *ArticleController) ListArticle(c *fiber.Ctx) error {
	var user User
    //c.BodyParser解析post
	if err := c.QueryParser(&user); err != nil {
		fmt.Print(err)
		printStackTrace(err)
		//return c.SendString("参数解析:"+err.Error())

	}



	if validationErr := Validator.Struct(&user); validationErr != nil {
		errmsg:= validate.ValidateMessage(user,validationErr)
		return c.SendString("参数解析:"+errmsg)
	}

 

4,测试效果:

参数解析:Cid:cid需要为整数,请重新输入

 

posted @ 2024-11-30 11:10  刘宏缔的架构森林  阅读(45)  评论(0编辑  收藏  举报