我是靠谱客的博主 明亮微笑,最近开发中收集的这篇文章主要介绍Iris——整合go-playground/validator参数校验Demo解决方案参考文章,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

源代码:https://gitee.com/shentuzhigang/mini-project/blob/master/iris_validator

解决方案

go.mod

require (
	github.com/Joker/hpp v1.0.0 // indirect
	github.com/go-playground/validator/v10 v10.6.1
	github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
	github.com/kataras/iris/v12 v12.2.0-alpha2.0.20210616105239-6d3884b0ceba
	github.com/mattn/go-colorable v0.1.8 // indirect
	github.com/smartystreets/goconvey v1.6.4 // indirect
	github.com/yudai/pp v2.0.1+incompatible // indirect
)

main.go

package main

import (
	"github.com/go-playground/validator/v10"
	"github.com/kataras/iris/v12"
)

func main() {
	app := iris.New()
	app.Validator = validator.New()
	app.Post("/user", profile)
	err := app.Listen(":8080")
	if err != nil {
		return
	}
}

//{"name" : "Marx", "age" : 202}
func profile(ctx iris.Context) {
	var user User
	err := ctx.ReadJSON(&user)
	if err != nil {
		_, err := ctx.JSON("error")
		if err != nil {
			return
		}
		return
	}

	_, err = ctx.JSON("ok")
	if err != nil {
		return
	}
}

type User struct {
	Name string `json:"name" validate:"required"`
	Age  uint8  `json:"age" validate:"required,gte=0,lte=130"`
}

参考文章

golang iris validator

最后

以上就是明亮微笑为你收集整理的Iris——整合go-playground/validator参数校验Demo解决方案参考文章的全部内容,希望文章能够帮你解决Iris——整合go-playground/validator参数校验Demo解决方案参考文章所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(32)

评论列表共有 0 条评论

立即
投稿
返回
顶部