Skip to content

Commit

Permalink
Merge pull request #575 from psampaz/datetime
Browse files Browse the repository at this point in the history
Add datetime validation
  • Loading branch information
deankarn authored Feb 9, 2020
2 parents b7430f8 + 912fcd1 commit c68441b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
23 changes: 21 additions & 2 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ var (
"hostname_port": isHostnamePort,
"lowercase": isLowercase,
"uppercase": isUppercase,
"datetime": isDatetime,
}
)

Expand Down Expand Up @@ -2020,8 +2021,9 @@ func isJSON(fl FieldLevel) bool {
if field.Kind() == reflect.String {
val := field.String()
return json.Valid([]byte(val))
}
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}

panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}

// isHostnamePort validates a <dns>:<port> combination for fields typically used for socket address.
Expand Down Expand Up @@ -2070,3 +2072,20 @@ func isUppercase(fl FieldLevel) bool {

panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}

// isDatetime is the validation function for validating if the current field's value is a valid datetime string.
func isDatetime(fl FieldLevel) bool {
field := fl.Field()
param := fl.Param()

if field.Kind() == reflect.String {
_, err := time.Parse(param, field.String())
if err != nil {
return false
}

return true
}

panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}
7 changes: 7 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,13 @@ can be used to valiate fields typically passed to sockets and connections.
Usage: hostname_port
Datetime
This validates that a string value is a valid datetime based on the supplied datetime format.
Supplied format must match the official Go time format layout as documented in https://golang.org/pkg/time/
Usage: datetime=2006-01-02
Alias Validators and Tags
NOTE: When returning an error, the tag returned in "FieldError" will be
Expand Down
17 changes: 16 additions & 1 deletion translations/en/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
{
tag: "json",
translation: "{0} must be a valid json string",
override: false,
override: false,
},
{
tag: "lowercase",
Expand All @@ -1336,6 +1336,21 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0} must be an uppercase string",
override: false,
},
{
tag: "datetime",
translation: "{0} does not match the {1} format",
override: false,
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {

t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())
if err != nil {
log.Printf("warning: error translating FieldError: %#v", fe)
return fe.(error).Error()
}

return t
},
},
}

for _, t := range translations {
Expand Down
6 changes: 6 additions & 0 deletions translations/en/en_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func TestTranslations(t *testing.T) {
JSONString string `validate:"json"`
LowercaseString string `validate:"lowercase"`
UppercaseString string `validate:"uppercase"`
Datetime string `validate:"datetime=2006-01-02"`
}

var test Test
Expand Down Expand Up @@ -195,6 +196,7 @@ func TestTranslations(t *testing.T) {

test.UniqueSlice = []string{"1234", "1234"}
test.UniqueMap = map[string]string{"key1": "1234", "key2": "1234"}
test.Datetime = "2008-Feb-01"

err = validate.Struct(test)
NotEqual(t, err, nil)
Expand Down Expand Up @@ -650,6 +652,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.UppercaseString",
expected: "UppercaseString must be an uppercase string",
},
{
ns: "Test.Datetime",
expected: "Datetime does not match the 2006-01-02 format",
},
}

for _, tt := range tests {
Expand Down
40 changes: 39 additions & 1 deletion validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9155,4 +9155,42 @@ func TestUppercaseValidation(t *testing.T) {
PanicMatches(t, func() {
_ = validate.Var(2, "uppercase")
}, "Bad field type int")
}

}

func TestDatetimeValidation(t *testing.T) {
tests := []struct {
value string `validate:"datetime=2006-01-02"`
tag string
expected bool
}{
{"2008-02-01", `datetime=2006-01-02`, true},
{"2008-Feb-01", `datetime=2006-01-02`, false},
}

validate := New()

for i, test := range tests {

errs := validate.Var(test.value, test.tag)

if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d datetime failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d datetime failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "datetime" {
t.Fatalf("Index: %d datetime failed Error: %s", i, errs)
}
}
}
}

PanicMatches(t, func() {
_ = validate.Var(2, "datetime")
}, "Bad field type int")
}

0 comments on commit c68441b

Please sign in to comment.