validator

package module
v0.0.0-...-8303d15 Latest Latest
Warning

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

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

README

Gin Gonic Custom Validator

Build Go Report Card GoDoc

This package was build to beautify Gin Gonic Binding validation error message for api. You can register your own custom validation and easily add customize its message as you like. Currently this package supports gopkg.in/go-playground/validator.v9

Available Scripts

Install

go get github.com/gobeam/custom-validator

Import in your code

import "github.com/gobeam/custom-validator"

Simple Examples

func main () {
router := gin.Default()

router.Use( validator.Errors())
	{
        router.GET("/some-method", SomeFunction)
	}

router.Run()
}

It has default validation message for email, required, min, max, len for extra other validation First you need to register your custom validation by process here

After you have successfully registered your custom validator you can register it when application starts in main function to beautify message by:

func main () {
router := gin.Default()

validate := []validator.ExtraValidation{
		{Tag: "test", Message:"%s is passed!"},
		{Tag: "unique", Message:"%s must be unique!"},
	}

validator.MakeExtraValidation(validate)

router.Use( validator.Errors())
	{
        router.GET("/some-method", SomeFunction)
	}

router.Run()
}

type TestModel struct {
    Name `json:"name" binding:required,unique`
}

func SomeFunction (c *gin.Context) {
    var model TestModel
	if err := c.ShouldBindBodyWith(&model, binding.JSON); err != nil {
		_ = c.AbortWithError(422, err).SetType(gin.ErrorTypeBind)
		return
	}
}

Here %s in field name which is automatically replaced later and it doesnot matter if the field name is camel case because it will be splitted automatically: for example: "firstName" will be outputted as "First name". You should name your field name with camelcase only and avoiding _ or underscore. If you have additional param like while specifying length we do len=10 and in message to get that param value just add another "%s" in message like "%s must be of length %s!" and it will be outputted like "Field name must be of length 10!"

Test

For testing run

go test

MIT License

Copyright (c) 2019

Documentation

Index

Constants

View Source
const (
	ErrorTypeErrorValidation string = "validator.ValidationErrors"
)

const list

Variables

View Source
var (
	ErrorInternalError = errors.New("whoops something went wrong")
)

var list

View Source
var ValidationObject = []ExtraValidation{
	{Tag: "required", Message: "%s is required!"},
	{Tag: "max", Message: "%s cannot be more than %s!"},
	{Tag: "min", Message: "%s must be minimum %s!"},
	{Tag: "email", Message: "Invalid email format!"},
	{Tag: "len", Message: "%s must be %s characters long!"},
}

ValidationObject initialize default Validation object

Functions

func Errors

func Errors() gin.HandlerFunc

Errors method collects all errors and submits them to Rollbar

func LcFirst

func LcFirst(str string) string

LcFirst method is to change lowercase word

func MakeExtraValidation

func MakeExtraValidation(v []ExtraValidation)

MakeExtraValidation method is for registering new validator

func Split

func Split(src string) string

Split method is to split camelcase letter

func UcFirst

func UcFirst(str string) string

UcFirst method is for uppercase first letter of word

func ValidationErrorToText

func ValidationErrorToText(e validator.FieldError) string

ValidationErrorToText method changes FieldError to string

Types

type ExtraValidation

type ExtraValidation struct {
	Tag     string
	Message string
}

ExtraValidation model

Jump to

Keyboard shortcuts

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