limit the max and min value of amount

This commit is contained in:
MaysWind
2020-12-17 01:33:07 +08:00
parent 24278cadb6
commit bdce9a7772
6 changed files with 68 additions and 12 deletions
+28 -6
View File
@@ -2,6 +2,7 @@ package utils
import (
"net/http"
"reflect"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
@@ -13,7 +14,7 @@ import (
func PrintSuccessResult(c *core.Context, result interface{}) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"result": result,
"result": result,
})
}
@@ -34,10 +35,10 @@ func PrintErrorResult(c *core.Context, err *errs.Error) {
}
c.AbortWithStatusJSON(err.HttpStatusCode, gin.H{
"success": false,
"errorCode": err.Code(),
"success": false,
"errorCode": err.Code(),
"errorMessage": errorMessage,
"path": c.Request.URL.Path,
"path": c.Request.URL.Path,
})
}
@@ -48,9 +49,17 @@ func getValidationErrorText(err validator.FieldError) string {
case "required":
return errs.GetParameterIsRequiredMessage(fieldName)
case "max":
return errs.GetParameterMustLessThanMessage(fieldName, err.Param())
if isIntegerParameter(err.Kind()) {
return errs.GetParameterMustLessThanMessage(fieldName, err.Param())
} else if isStringParameter(err.Kind()) {
return errs.GetParameterMustLessThanCharsMessage(fieldName, err.Param())
}
case "min":
return errs.GetParameterMustMoreThanMessage(fieldName, err.Param())
if isIntegerParameter(err.Kind()) {
return errs.GetParameterMustMoreThanMessage(fieldName, err.Param())
} else if isStringParameter(err.Kind()) {
return errs.GetParameterMustMoreThanCharsMessage(fieldName, err.Param())
}
case "len":
return errs.GetParameterLengthNotEqualMessage(fieldName, err.Param())
case "notBlank":
@@ -67,3 +76,16 @@ func getValidationErrorText(err validator.FieldError) string {
return errs.GetParameterInvalidMessage(fieldName)
}
func isIntegerParameter(kind reflect.Kind) bool {
switch kind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return true
default:
return false
}
}
func isStringParameter(kind reflect.Kind) bool {
return kind == reflect.String
}