Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gobeam/custom-validator
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.
https://github.com/gobeam/custom-validator
beautify custom-validation gin golang golang-package gonic middleware v9 validation-messages validator
Last synced: about 2 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/gobeam/custom-validator
- Owner: gobeam
- License: mit
- Created: 2019-01-10T08:21:33.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-26T15:35:39.000Z (over 4 years ago)
- Last Synced: 2024-10-15T07:04:36.852Z (2 months ago)
- Topics: beautify, custom-validation, gin, golang, golang-package, gonic, middleware, v9, validation-messages, validator
- Language: Go
- Homepage: https://godoc.org/github.com/roshanr83/validator
- Size: 36.1 KB
- Stars: 14
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Gin Gonic Custom Validator
[![Build][Build-Status-Image]][Build-Status-Url] [![Go Report Card](https://goreportcard.com/badge/github.com/gobeam/custom-validator?branch=master)](https://goreportcard.com/report/github.com/gobeam/custom-validator) [![GoDoc][godoc-image]][godoc-url]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
```bash
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](https://github.com/gin-gonic/gin#custom-validators)
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```bash
go test
```[Build-Status-Url]: https://travis-ci.org/gobeam/custom-validator
[Build-Status-Image]: https://travis-ci.org/gobeam/custom-validator.svg?branch=master
[godoc-url]: https://pkg.go.dev/github.com/gobeam/custom-validator?tab=doc
[godoc-image]: https://godoc.org/github.com/gobeam/custom-validator?status.svg
## MIT License```
Copyright (c) 2019
```