simple_valid

package module
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2020 License: MIT Imports: 11 Imported by: 0

README

simple_valid

a simple paramater validator for iris , use middleware

use see test

// Define sv
	sv := simple_valid.New("sv", func(err error, ctx context.Context) {
		ctx.StatusCode(iris.StatusBadRequest)
		_, _ = ctx.JSON(iris.Map{"detail": err.Error()})
		return
	})
// Define req valid struct
	type req struct {
		Name string `json:"name" url:"name" comment:"name" validate:"required"`
	}
// Use middleware
	app.Get("/", sv.Run(new(req)), func(ctx context.Context) {
		req := ctx.Values().Get("sv").(*req) // <- this get req data 
		_, _ = ctx.JSON(iris.Map{"name": req.Name})
	})

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Info    *log.Logger
	Warning *log.Logger
	Error   *log.Logger
)

Functions

func InitValidator

func InitValidator()

InitValidator 初始化验证器

Types

type CanCheck

type CanCheck interface {
	Check() error
}

CanCheck 如果需要特殊校验,可以实现验证接口,或者通过自定义tag标签实现

type MyValidator

type MyValidator struct {
	Validate *validator.Validate
	Trans    ut.Translator
}

MyValidator 自定义验证器

var GlobalValidator MyValidator

GlobalValidator 全局验证器

func (*MyValidator) Check

func (m *MyValidator) Check(value interface{}) error

Check 验证器通用验证方法

type ReqValid

type ReqValid struct {
	Valid      interface{}
	Mode       string
	FailFunc   func(err error, ctx context.Context)
	ContextKey string
}

请求验证器

func New

func New(cKey string, fail func(err error, ctx context.Context)) ReqValid

func (*ReqValid) Run

func (c *ReqValid) Run(valid interface{}, mode ...string) context.Handler

func (*ReqValid) Serve

func (c *ReqValid) Serve(ctx context.Context)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL