diff --git a/cmd/webserver.go b/cmd/webserver.go index 17dc8a13..dea8d384 100644 --- a/cmd/webserver.go +++ b/cmd/webserver.go @@ -74,7 +74,7 @@ func startWebServer(c *cli.Context) error { _ = v.RegisterValidation("validUsername", validators.ValidUsername) _ = v.RegisterValidation("validEmail", validators.ValidEmail) _ = v.RegisterValidation("validCurrency", validators.ValidCurrency) - _ = v.RegisterValidation("validRGBColor", validators.ValidRGBColor) + _ = v.RegisterValidation("validHexRGBColor", validators.ValidHexRGBColor) } router.NoRoute(bindApi(api.Default.ApiNotFound)) diff --git a/pkg/errs/global.go b/pkg/errs/global.go index 6b86aeec..e8cfcd47 100644 --- a/pkg/errs/global.go +++ b/pkg/errs/global.go @@ -48,3 +48,7 @@ func GetParameterInvalidEmailMessage(field string) string { func GetParameterInvalidCurrencyMessage(field string) string { return fmt.Sprintf("parameter \"%s\" is invalid currency", field) } + +func GetParameterInvalidHexRGBColorMessage(field string) string { + return fmt.Sprintf("parameter \"%s\" is invalid color", field) +} diff --git a/pkg/models/account.go b/pkg/models/account.go index a5abd00d..13ceb01a 100644 --- a/pkg/models/account.go +++ b/pkg/models/account.go @@ -47,7 +47,7 @@ type AccountCreateRequest struct { Category AccountCategory `json:"category" binding:"required"` Type AccountType `json:"type" binding:"required"` Icon int64 `json:"icon,string" binding:"required,min=1"` - Color string `json:"color" binding:"required,len=6,validRGBColor"` + Color string `json:"color" binding:"required,len=6,validHexRGBColor"` Currency string `json:"currency" binding:"required,len=3,validCurrency"` Comment string `json:"comment" binding:"max=255"` SubAccounts []*AccountCreateRequest `json:"subAccounts" binding:"omitempty"` @@ -62,7 +62,7 @@ type AccountModifyRequest struct { Name string `json:"name" binding:"required,notBlank,max=32"` Category AccountCategory `json:"category" binding:"required"` Icon int64 `json:"icon,string" binding:"min=1"` - Color string `json:"color" binding:"required,len=6,validRGBColor"` + Color string `json:"color" binding:"required,len=6,validHexRGBColor"` Comment string `json:"comment" binding:"max=255"` Hidden bool `json:"hidden"` SubAccounts []*AccountModifyRequest `json:"subAccounts" binding:"omitempty"` diff --git a/pkg/utils/api.go b/pkg/utils/api.go index 103beb38..68ce0109 100644 --- a/pkg/utils/api.go +++ b/pkg/utils/api.go @@ -61,6 +61,8 @@ func getValidationErrorText(err validator.FieldError) string { return errs.GetParameterInvalidEmailMessage(fieldName) case "validCurrency": return errs.GetParameterInvalidCurrencyMessage(fieldName) + case "validHexRGBColor": + return errs.GetParameterInvalidHexRGBColorMessage(fieldName) } return errs.GetParameterInvalidMessage(fieldName) diff --git a/pkg/utils/regexps.go b/pkg/utils/regexps.go index da15073f..bac76866 100644 --- a/pkg/utils/regexps.go +++ b/pkg/utils/regexps.go @@ -5,7 +5,7 @@ import "regexp" var ( UsernamePattern = regexp.MustCompile("^(?i)[a-z0-9_-]+$") EmailPattern = regexp.MustCompile("^(?i)(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])$") - RGBColorPattern = regexp.MustCompile("^(?i)([0-9a-f]{6}|[0-9a-f]{3})$") + HexRGBColorPattern = regexp.MustCompile("^(?i)([0-9a-f]{6}|[0-9a-f]{3})$") ) func IsValidUsername(username string) bool { @@ -16,6 +16,6 @@ func IsValidEmail(email string) bool { return EmailPattern.MatchString(email) } -func IsValidRGBColor(color string) bool { - return RGBColorPattern.MatchString(color) +func IsValidHexRGBColor(color string) bool { + return HexRGBColorPattern.MatchString(color) } diff --git a/pkg/validators/rgb_color.go b/pkg/validators/hex_rgb_color.go similarity index 68% rename from pkg/validators/rgb_color.go rename to pkg/validators/hex_rgb_color.go index 2ec6e3fd..89bb6a43 100644 --- a/pkg/validators/rgb_color.go +++ b/pkg/validators/hex_rgb_color.go @@ -6,9 +6,9 @@ import ( "github.com/mayswind/lab/pkg/utils" ) -func ValidRGBColor(fl validator.FieldLevel) bool { +func ValidHexRGBColor(fl validator.FieldLevel) bool { if value, ok := fl.Field().Interface().(string); ok { - if utils.IsValidRGBColor(value) { + if utils.IsValidHexRGBColor(value) { return true } } diff --git a/src/lib/i18n.js b/src/lib/i18n.js index b57b15fa..45be8b91 100644 --- a/src/lib/i18n.js +++ b/src/lib/i18n.js @@ -88,6 +88,14 @@ const parameterizedErrors = [ field: 'parameter', localized: true }] + }, + { + localeKey: 'parameter invalid color', + regex: /^parameter "(\w+)" is invalid color$/, + parameters: [{ + field: 'parameter', + localized: true + }] } ]; diff --git a/src/locales/en.js b/src/locales/en.js index 452d3c78..821a9add 100644 --- a/src/locales/en.js +++ b/src/locales/en.js @@ -236,6 +236,7 @@ export default { 'parameter invalid username format': '{parameter} is invalid format', 'parameter invalid email format': '{parameter} is invalid format', 'parameter invalid currency': '{parameter} is invalid format', + 'parameter invalid color': '{parameter} is invalid format', }, 'OK': 'OK', 'Cancel': 'Cancel', diff --git a/src/locales/zh_Hans.js b/src/locales/zh_Hans.js index 9565b5be..3a1dcf58 100644 --- a/src/locales/zh_Hans.js +++ b/src/locales/zh_Hans.js @@ -236,6 +236,7 @@ export default { 'parameter invalid username format': '{parameter}格式错误', 'parameter invalid email format': '{parameter}格式错误', 'parameter invalid currency': '{parameter}格式错误', + 'parameter invalid color': '{parameter}格式错误', }, 'OK': '确定', 'Cancel': '取消',