validator

package
v1.14.5 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2025 License: MIT Imports: 3 Imported by: 0

README

validator

validator is based on validator library. It provides request parameter validation for gin.


Example of use

package main

import (
    "net/http"

    "github.com/go-dev-frame/sponge/pkg/gin/validator"

    "github.com/gin-gonic/gin"
    "github.com/gin-gonic/gin/binding"
)

func main() {
	r := gin.Default()
	binding.Validator = validator.Init()
	
	r.POST("/create_user", CreateUser)
	
	r.Run(":8080")
}

type createUserRequest struct {
	Name  string `json:"name" form:"name" binding:"required"`
	Password string `json:"password" form:"password" binding:"required"`
	Age   int    `json:"age" form:"age" binding:"gte=0,lte=120"`
	Email string `json:"email" form:"email" binding:"email"`
}

func CreateUser(c *gin.Context) {
	form := &createUserRequest{}
	err := c.ShouldBindJSON(form)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"msg": "ok"})
}

Documentation

Overview

Package validator is gin request parameter check library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CustomValidator

type CustomValidator struct {
	Validate *valid.Validate
	// contains filtered or unexported fields
}

CustomValidator Custom valid objects

func Init

func Init() *CustomValidator

Init validator instance, used to gin request parameter check

func NewCustomValidator

func NewCustomValidator() *CustomValidator

NewCustomValidator Instantiate

func (*CustomValidator) Engine

func (v *CustomValidator) Engine() interface{}

Engine set tag name "binding", which is implementing the validator interface of the gin framework

func (*CustomValidator) ValidateStruct

func (v *CustomValidator) ValidateStruct(obj interface{}) error

ValidateStruct validates a struct or slice/array

Jump to

Keyboard shortcuts

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