Skip to content

Commit 1f676c7

Browse files
author
Dean Karn
authored
Merge branch 'master' into master
2 parents a1ac82a + fa149de commit 1f676c7

File tree

7 files changed

+2172
-2
lines changed

7 files changed

+2172
-2
lines changed

baked_in.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ var (
167167
"url_encoded": isURLEncoded,
168168
"dir": isDir,
169169
"hostname_port": isHostnamePort,
170+
"lowercase": isLowercase,
171+
"uppercase": isUppercase,
170172
}
171173
)
172174

@@ -2027,3 +2029,31 @@ func isHostnamePort(fl FieldLevel) bool {
20272029
}
20282030
return true
20292031
}
2032+
2033+
// isLowercase is the validation function for validating if the current field's value is a lowercase string.
2034+
func isLowercase(fl FieldLevel) bool {
2035+
field := fl.Field()
2036+
2037+
if field.Kind() == reflect.String {
2038+
if field.String() == "" {
2039+
return false
2040+
}
2041+
return field.String() == strings.ToLower(field.String())
2042+
}
2043+
2044+
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
2045+
}
2046+
2047+
// isUppercase is the validation function for validating if the current field's value is an uppercase string.
2048+
func isUppercase(fl FieldLevel) bool {
2049+
field := fl.Field()
2050+
2051+
if field.Kind() == reflect.String {
2052+
if field.String() == "" {
2053+
return false
2054+
}
2055+
return field.String() == strings.ToUpper(field.String())
2056+
}
2057+
2058+
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
2059+
}

doc.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ handy in ignoring embedded structs from being validated. (Usage: -)
158158
Or Operator
159159
160160
This is the 'or' operator allowing multiple validators to be used and
161-
accepted. (Usage: rbg|rgba) <-- this would allow either rgb or rgba
161+
accepted. (Usage: rgb|rgba) <-- this would allow either rgb or rgba
162162
colors to be accepted. This can also be combined with 'and' for example
163163
( Usage: omitempty,rgb|rgba)
164164
@@ -641,6 +641,18 @@ hashtag (#)
641641
642642
Usage: hexcolor
643643
644+
Lowercase String
645+
646+
This validates that a string value contains only lowercase characters. An empty string is not a valid lowercase string.
647+
648+
Usage: lowercase
649+
650+
Uppercase String
651+
652+
This validates that a string value contains only uppercase characters. An empty string is not a valid uppercase string.
653+
654+
Usage: uppercase
655+
644656
RGB String
645657
646658
This validates that a string value contains a valid rgb color

translations/en/en.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,16 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
13211321
return s
13221322
},
13231323
},
1324+
{
1325+
tag: "lowercase",
1326+
translation: "{0} must be a lowercase string",
1327+
override: false,
1328+
},
1329+
{
1330+
tag: "uppercase",
1331+
translation: "{0} must be an uppercase string",
1332+
override: false,
1333+
},
13241334
}
13251335

13261336
for _, t := range translations {

translations/en/en_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"testing"
55
"time"
66

7+
. "github.com/go-playground/assert/v2"
78
english "github.com/go-playground/locales/en"
89
ut "github.com/go-playground/universal-translator"
9-
. "github.com/go-playground/assert/v2"
1010
"github.com/go-playground/validator/v10"
1111
)
1212

@@ -141,6 +141,8 @@ func TestTranslations(t *testing.T) {
141141
UniqueSlice []string `validate:"unique"`
142142
UniqueArray [3]string `validate:"unique"`
143143
UniqueMap map[string]string `validate:"unique"`
144+
LowercaseString string `validate:"lowercase"`
145+
UppercaseString string `validate:"uppercase"`
144146
}
145147

146148
var test Test
@@ -183,6 +185,9 @@ func TestTranslations(t *testing.T) {
183185

184186
test.MultiByte = "1234feerf"
185187

188+
test.LowercaseString = "ABCDEFG"
189+
test.UppercaseString = "abcdefg"
190+
186191
s := "toolong"
187192
test.StrPtrMaxLen = &s
188193
test.StrPtrLen = &s
@@ -632,6 +637,14 @@ func TestTranslations(t *testing.T) {
632637
ns: "Test.UniqueMap",
633638
expected: "UniqueMap must contain unique values",
634639
},
640+
{
641+
ns: "Test.LowercaseString",
642+
expected: "LowercaseString must be a lowercase string",
643+
},
644+
{
645+
ns: "Test.UppercaseString",
646+
expected: "UppercaseString must be an uppercase string",
647+
},
635648
}
636649

637650
for _, tt := range tests {

0 commit comments

Comments
 (0)