validation

package module
v0.0.0-...-27d25fc Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2020 License: MIT Imports: 3 Imported by: 0

README

Chinese

gin-validation

This is a BaseController and Validation for Gin framework.

It uses govalidator to support the request param validaton. It also provides baseController that other handler struct can combine this BaseController.

Usage

Download and install using go module:

export GO111MODULE=on
go get -u github.com/shuxnhs/gin-validation

Import it in your code:

import (
    validation "github.com/shuxnhs/gin-validation"
)

Example

Please see the example Controller and you can use Declarative parameter validation to validator the request data.

package main

import (
	"github.com/gin-gonic/gin"
	validation "github.com/shuxnhs/gin-validation"
	"github.com/shuxnhs/gin-validation/formatter"
)

type ExampleController struct {
	validation.BaseController
}

func (e *ExampleController) Ping(ctx *gin.Context) {
	ruleMap := map[string]formatter.Rule{
		"objectId": {"name": "object_id", "type": formatter.ValidTypeInt, "required": true, "rule": "", "default": 123},
		"objectName": {"name": "object_name", "type": formatter.ValidTypeString, "required": true,
			"rule": "length(1|10),in(string1|string2|...|stringN)"},
	}
	paramMap := e.Rules(ctx, ruleMap)
	if ctx.IsAborted() {
		return
	}
	ctx.JSON(200, paramMap["objectId"])
}

explain

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseController

type BaseController struct{}

func (*BaseController) Rules

func (bc *BaseController) Rules(ctx *gin.Context, ruleMap map[string]formatter.Rule) map[string]interface{}

检验参数实现

type ValidatorParam

type ValidatorParam interface {
	Rules(ctx *gin.Context, ruleMap map[string]formatter.Rule) map[string]interface{}
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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