From bdce9a7772a8aca7613d83da84ce0cc4eeb27c35 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Thu, 17 Dec 2020 01:33:07 +0800 Subject: [PATCH] limit the max and min value of amount --- pkg/errs/global.go | 8 ++++++++ pkg/models/transaction.go | 8 ++++---- pkg/utils/api.go | 34 ++++++++++++++++++++++++++++------ src/lib/i18n.js | 26 ++++++++++++++++++++++++-- src/locales/en.js | 2 ++ src/locales/zh_Hans.js | 2 ++ 6 files changed, 68 insertions(+), 12 deletions(-) diff --git a/pkg/errs/global.go b/pkg/errs/global.go index f9350aad..28b69d53 100644 --- a/pkg/errs/global.go +++ b/pkg/errs/global.go @@ -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) } diff --git a/pkg/models/transaction.go b/pkg/models/transaction.go index 747e004d..17bff999 100644 --- a/pkg/models/transaction.go +++ b/pkg/models/transaction.go @@ -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"` } diff --git a/pkg/utils/api.go b/pkg/utils/api.go index 68ce0109..a72319c2 100644 --- a/pkg/utils/api.go +++ b/pkg/utils/api.go @@ -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 +} diff --git a/src/lib/i18n.js b/src/lib/i18n.js index d89f7abc..fe688979 100644 --- a/src/lib/i18n.js +++ b/src/lib/i18n.js @@ -25,8 +25,19 @@ const parameterizedErrors = [ }] }, { - localeKey: 'parameter too long', + localeKey: 'parameter too large', regex: /^parameter "(\w+)" must be less than (\d+)$/, + parameters: [{ + field: 'parameter', + localized: true + }, { + field: 'number', + localized: false + }] + }, + { + localeKey: 'parameter too long', + regex: /^parameter "(\w+)" must be less than (\d+) characters$/, parameters: [{ field: 'parameter', localized: true @@ -36,8 +47,19 @@ const parameterizedErrors = [ }] }, { - localeKey: 'parameter too short', + localeKey: 'parameter too small', regex: /^parameter "(\w+)" must be more than (\d+)$/, + parameters: [{ + field: 'parameter', + localized: true + }, { + field: 'number', + localized: false + }] + }, + { + localeKey: 'parameter too short', + regex: /^parameter "(\w+)" must be more than (\d+) characters$/, parameters: [{ field: 'parameter', localized: true diff --git a/src/locales/en.js b/src/locales/en.js index 8e4057fd..fea3e193 100644 --- a/src/locales/en.js +++ b/src/locales/en.js @@ -364,7 +364,9 @@ export default { 'parameterizedError': { 'parameter invalid': '{parameter} is invalid', 'parameter required': '{parameter} is required', + 'parameter too large': '{parameter} must be at most {number}', 'parameter too long': '{parameter} must be at most {length} characters', + 'parameter too small': '{parameter} must be at least {number}', 'parameter too short': '{parameter} must be at least {length} characters', 'parameter length not equal': '{parameter} must be {length} characters', 'parameter cannot be blank': '{parameter} cannot be blank', diff --git a/src/locales/zh_Hans.js b/src/locales/zh_Hans.js index 3d40e2c4..cd8d670a 100644 --- a/src/locales/zh_Hans.js +++ b/src/locales/zh_Hans.js @@ -364,7 +364,9 @@ export default { 'parameterizedError': { 'parameter invalid': '{parameter}无效', 'parameter required': '{parameter}为必填项', + 'parameter too large': '{parameter}必须小于{number}', 'parameter too long': '{parameter}必须小于{length}个字符', + 'parameter too small': '{parameter}必须大于{number}', 'parameter too short': '{parameter}必须大于{length}个字符', 'parameter length not equal': '{parameter}必须等于{length}个字符', 'parameter cannot be blank': '{parameter}不能为空',