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
+8
View File
@@ -28,10 +28,18 @@ func GetParameterMustLessThanMessage(field string, param string) string {
return fmt.Sprintf("parameter \"%s\" must be less than %s", field, param)
}
func GetParameterMustLessThanCharsMessage(field string, param string) string {
return fmt.Sprintf("parameter \"%s\" must be less than %s characters", field, param)
}
func GetParameterMustMoreThanMessage(field string, param string) string {
return fmt.Sprintf("parameter \"%s\" must be more than %s", field, param)
}
func GetParameterMustMoreThanCharsMessage(field string, param string) string {
return fmt.Sprintf("parameter \"%s\" must be more than %s characters", field, param)
}
func GetParameterLengthNotEqualMessage(field string, param string) string {
return fmt.Sprintf("parameter \"%s\" length is not equal to %s", field, param)
}
+4 -4
View File
@@ -34,8 +34,8 @@ type TransactionCreateRequest struct {
Time int64 `json:"time" binding:"required,min=1"`
SourceAccountId int64 `json:"sourceAccountId,string" binding:"required,min=1"`
DestinationAccountId int64 `json:"destinationAccountId,string" binding:"required,min=1"`
SourceAmount int64 `json:"sourceAmount"`
DestinationAmount int64 `json:"destinationAmount"`
SourceAmount int64 `json:"sourceAmount" binding:"min=-99999999999,max=99999999999"`
DestinationAmount int64 `json:"destinationAmount" binding:"min=-99999999999,max=99999999999"`
TagIds []int64 `json:"tagIds,string"`
Comment string `json:"comment" binding:"max=255"`
}
@@ -46,8 +46,8 @@ type TransactionModifyRequest struct {
Time int64 `json:"time" binding:"required,min=1"`
SourceAccountId int64 `json:"sourceAccountId,string" binding:"required,min=1"`
DestinationAccountId int64 `json:"destinationAccountId,string" binding:"required,min=1"`
SourceAmount int64 `json:"sourceAmount"`
DestinationAmount int64 `json:"destinationAmount"`
SourceAmount int64 `json:"sourceAmount" binding:"min=-99999999999,max=99999999999"`
DestinationAmount int64 `json:"destinationAmount" binding:"min=-99999999999,max=99999999999"`
TagIds []int64 `json:"tagIds,string"`
Comment string `json:"comment" binding:"max=255"`
}
+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
}