2023-09-08 22:25:38 +07:00
|
|
|
package api
|
|
|
|
|
2023-09-13 11:30:15 +07:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-08-19 07:40:01 +07:00
|
|
|
"reflect"
|
2023-09-13 11:30:15 +07:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
)
|
|
|
|
|
|
|
|
type APIValidationError struct {
|
|
|
|
Field string `json:"field"`
|
|
|
|
Msg string `json:"msg"`
|
|
|
|
}
|
|
|
|
|
2023-09-19 21:49:05 +07:00
|
|
|
func validationErrorMsg(field string, param string, tag string, typeName string) string {
|
2023-09-13 11:30:15 +07:00
|
|
|
switch tag {
|
|
|
|
case "min":
|
2023-09-19 21:49:05 +07:00
|
|
|
if typeName == "int16" || typeName == "int64" {
|
|
|
|
return fmt.Sprintf("%s(number) value min %s", field, param)
|
|
|
|
}
|
2023-09-17 16:31:40 +07:00
|
|
|
return fmt.Sprintf("%s character min %s character", field, param)
|
|
|
|
case "max":
|
2023-09-19 21:49:05 +07:00
|
|
|
if typeName == "int16" || typeName == "int64" {
|
|
|
|
return fmt.Sprintf("%s(number) value max is %s", field, param)
|
|
|
|
}
|
2023-09-17 16:31:40 +07:00
|
|
|
return fmt.Sprintf("%s character max %s character", field, param)
|
2023-09-13 11:30:15 +07:00
|
|
|
case "required":
|
|
|
|
return fmt.Sprintf("%s is %s", field, tag)
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%s %s", field, tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ValidationErrorResponse(err error) gin.H {
|
|
|
|
var ves validator.ValidationErrors
|
|
|
|
var temp []APIValidationError
|
|
|
|
if errors.As(err, &ves) {
|
|
|
|
out := make([]APIValidationError, len(ves))
|
|
|
|
for i, ve := range ves {
|
2023-09-19 21:49:05 +07:00
|
|
|
out[i] = APIValidationError{ve.Field(), validationErrorMsg(ve.Field(), ve.Param(), ve.ActualTag(), ve.Type().Name())}
|
2023-09-13 11:30:15 +07:00
|
|
|
}
|
|
|
|
temp = out
|
|
|
|
}
|
|
|
|
return gin.H{
|
|
|
|
"message": "Validation error",
|
|
|
|
"errors": temp,
|
|
|
|
}
|
|
|
|
}
|
2023-09-08 22:25:38 +07:00
|
|
|
|
2024-08-19 07:40:01 +07:00
|
|
|
func ValidationErrorResponseV2(err error, s interface{}, tagName string) gin.H {
|
|
|
|
var ves validator.ValidationErrors
|
|
|
|
var temp []APIValidationError
|
|
|
|
if errors.As(err, &ves) {
|
|
|
|
out := make([]APIValidationError, len(ves))
|
|
|
|
for i, ve := range ves {
|
|
|
|
fieldName := ve.Field()
|
|
|
|
field, _ := reflect.TypeOf(s).FieldByName(fieldName)
|
|
|
|
tag := field.Tag.Get(tagName)
|
|
|
|
out[i] = APIValidationError{tag, validationErrorMsg(tag, ve.Param(), ve.ActualTag(), ve.Type().Name())}
|
|
|
|
}
|
|
|
|
temp = out
|
|
|
|
}
|
|
|
|
return gin.H{
|
|
|
|
"message": "Validation error",
|
|
|
|
"errors": temp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 17:08:17 +07:00
|
|
|
func ErrorResponse(err error, msg string) gin.H {
|
2023-09-08 22:25:38 +07:00
|
|
|
return gin.H{
|
|
|
|
"error": err.Error(),
|
|
|
|
"message": msg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 17:08:17 +07:00
|
|
|
func ValidResponse(data interface{}, msg string) gin.H {
|
2023-09-08 22:25:38 +07:00
|
|
|
return gin.H{
|
|
|
|
"message": msg,
|
|
|
|
"data": data,
|
|
|
|
}
|
|
|
|
}
|