Giter Club home page Giter Club logo

gobeam / custom-validator Goto Github PK

View Code? Open in Web Editor NEW
14.0 3.0 2.0 37 KB

This package was build to beautify the Gin Gonic Binding validation error message for API. You can register your custom validation and easily add customize its message as you like.

Home Page: https://godoc.org/github.com/roshanr83/validator

License: MIT License

Go 100.00%
custom-validation gin gonic beautify validation-messages golang golang-package middleware validator v9

custom-validator's Introduction

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

custom-validator's People

Contributors

gobeam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.