Skip to content

Gin (Golang) package for customization validation errors

License

Notifications You must be signed in to change notification settings

Kesha005/validate_err

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VEC validation error customization

Gin (Golang) validation error package for easy request validation by Saparov

Installing

Install from repository

go get github.com/Kesha005/validate_err

Usage

Repository is not validation package, it's for customization for validation error Gin framework

To use custom messages only we should create map with field and that's field errors

Example:

  
package handlers

import (
        "net/http"  
        "github.com/gin-gonic/gin"
        "gorm.io/gorm"
        "github.com/Kesha005/validate_err")


type BookRequest struct{
      Name string `json:"name" binding:"required"`
      Author string `json:"author"`
}

var validation_err = map[string]string{
      "name.required" :"Name field is required",
      "author.required":"Author field is required",
}

func (db datab) Store(ctx *gin.Context){
      body := BookRequest{}
      if  err := ctx.ShouldBindJSON(&body);err!=nil{
	    data := validate_err.GetErr(err,&validation_err)
	    ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errors": data})
	    return 
    }
    var book models.Book

    book.Name = body.Name
    book.Author = body.Author

    if res := db.Db.Create(&book);res.Error!=nil{
	  ctx.AbortWithError(http.StatusForbidden,res.Error)
	  return 
  }
    ctx.JSON(http.StatusAccepted,&book)

}

About

Gin (Golang) package for customization validation errors

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages