Skip to content

Instantly share code, notes, and snippets.

@Mr-Malomz
Created October 21, 2022 15:16
Show Gist options
  • Save Mr-Malomz/5690215ccf4add118f029515be9bbbc0 to your computer and use it in GitHub Desktop.
Save Mr-Malomz/5690215ccf4add118f029515be9bbbc0 to your computer and use it in GitHub Desktop.
package api
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
)
type jsonResponse struct {
Status int `json:"status"`
Message string `json:"message"`
Data any `json:"data"`
}
var validate = validator.New()
func (app *Config) validateBody(c *gin.Context, data any) error {
//validate the request body
if err := c.BindJSON(&data); err != nil {
return err
}
//use the validator library to validate required fields
if err := validate.Struct(&data); err != nil {
return err
}
return nil
}
func (app *Config) writeJSON(c *gin.Context, status int, data any) {
c.JSON(status, jsonResponse{Status: status, Message: "success", Data: data})
}
func (app *Config) errorJSON(c *gin.Context, err error, status ...int) {
statusCode := http.StatusBadRequest
if len(status) > 0 {
statusCode = status[0]
}
c.JSON(statusCode, jsonResponse{Status: statusCode, Message: err.Error()})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment