Add isLowercase and isUppercase valitation

pull/572/head
Pantelis Sampaziotis 5 years ago
parent fb6c45823a
commit 5b51bff4e6
  1. 24
      baked_in.go
  2. 12
      doc.go
  3. 10
      translations/en/en.go
  4. 15
      translations/en/en_test.go
  5. 74
      validator_test.go

@ -166,6 +166,8 @@ var (
"html_encoded": isHTMLEncoded,
"url_encoded": isURLEncoded,
"dir": isDir,
"lowercase": isLowercase,
"uppercase": isUppercase,
}
)
@ -2007,3 +2009,25 @@ func isDir(fl FieldLevel) bool {
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}
// isLowercase is the validation function for validating if the current field's value is a lowercase string.
func isLowercase(fl FieldLevel) bool {
field := fl.Field()
if field.Kind() == reflect.String {
return field.String() == strings.ToLower(field.String())
}
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}
// isUppercase is the validation function for validating if the current field's value is an uppercase string.
func isUppercase(fl FieldLevel) bool {
field := fl.Field()
if field.Kind() == reflect.String {
return field.String() == strings.ToUpper(field.String())
}
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}

@ -641,6 +641,18 @@ hashtag (#)
Usage: hexcolor
Lowercase String
This validates that a string value contains only lowercase characters. An empty string is considered a valid lowercase string.
Usage: lowercase
Uppercase String
This validates that a string value contains only uppercase characters. An empty string is considered a valid uppercase string.
Usage: uppercase
RGB String
This validates that a string value contains a valid rgb color

@ -1321,6 +1321,16 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return s
},
},
{
tag: "lowercase",
translation: "{0} must be a lowercase string",
override: false,
},
{
tag: "uppercase",
translation: "{0} must be an uppercase string",
override: false,
},
}
for _, t := range translations {

@ -4,9 +4,9 @@ import (
"testing"
"time"
. "github.com/go-playground/assert/v2"
english "github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator"
. "github.com/go-playground/assert/v2"
"github.com/go-playground/validator/v10"
)
@ -141,6 +141,8 @@ func TestTranslations(t *testing.T) {
UniqueSlice []string `validate:"unique"`
UniqueArray [3]string `validate:"unique"`
UniqueMap map[string]string `validate:"unique"`
LowercaseString string `validate:"lowercase"`
UppercaseString string `validate:"uppercase"`
}
var test Test
@ -183,6 +185,9 @@ func TestTranslations(t *testing.T) {
test.MultiByte = "1234feerf"
test.LowercaseString = "ABCDEFG"
test.UppercaseString = "abcdefg"
s := "toolong"
test.StrPtrMaxLen = &s
test.StrPtrLen = &s
@ -632,6 +637,14 @@ func TestTranslations(t *testing.T) {
ns: "Test.UniqueMap",
expected: "UniqueMap must contain unique values",
},
{
ns: "Test.LowercaseString",
expected: "LowercaseString must be a lowercase string",
},
{
ns: "Test.UppercaseString",
expected: "UppercaseString must be an uppercase string",
},
}
for _, tt := range tests {

@ -9003,3 +9003,77 @@ func TestGetTag(t *testing.T) {
Equal(t, errs, nil)
Equal(t, tag, "mytag")
}
func TestLowercaseValidation(t *testing.T) {
tests := []struct {
param string
expected bool
}{
{`abcdefg`, true},
{`Abcdefg`, false},
{"", true},
}
validate := New()
for i, test := range tests {
errs := validate.Var(test.param, "lowercase")
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d lowercase failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d lowercase failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "lowercase" {
t.Fatalf("Index: %d lowercase failed Error: %s", i, errs)
}
}
}
}
PanicMatches(t, func() {
_ = validate.Var(2, "lowercase")
}, "Bad field type int")
}
func TestUppercaseValidation(t *testing.T) {
tests := []struct {
param string
expected bool
}{
{`ABCDEFG`, true},
{`aBCDEFG`, false},
{"", true},
}
validate := New()
for i, test := range tests {
errs := validate.Var(test.param, "uppercase")
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d uppercase failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d uppercase failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "uppercase" {
t.Fatalf("Index: %d uppercase failed Error: %s", i, errs)
}
}
}
}
PanicMatches(t, func() {
_ = validate.Var(2, "uppercase")
}, "Bad field type int")
}

Loading…
Cancel
Save