Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for time zone validation #641

Merged
merged 2 commits into from
Sep 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ var (
"lowercase": isLowercase,
"uppercase": isUppercase,
"datetime": isDatetime,
"timezone": isTimeZone,
}
)

Expand Down Expand Up @@ -2096,3 +2097,29 @@ func isDatetime(fl FieldLevel) bool {

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

// isTimeZone is the validation function for validating if the current field's value is a valid time zone string.
func isTimeZone(fl FieldLevel) bool {
field := fl.Field()

if field.Kind() == reflect.String {
// empty value is converted to UTC by time.LoadLocation but disallow it as it is not a valid time zone name
if field.String() == "" {
return false
}

// Local value is converted to the current system time zone by time.LoadLocation but disallow it as it is not a valid time zone name
if strings.ToLower(field.String()) == "local" {
return false
}

_, err := time.LoadLocation(field.String())
if err != nil {
return false
}

return true
}

panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}
8 changes: 8 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,14 @@ Supplied format must match the official Go time format layout as documented in h

Usage: datetime=2006-01-02

TimeZone

This validates that a string value is a valid time zone based on the time zone database present on the system.
Although empty value and Local value are allowed by time.LoadLocation golang function, they are not allowed by this validator.
More information on https://golang.org/pkg/time/#LoadLocation

Usage: timezone

Alias Validators and Tags

NOTE: When returning an error, the tag returned in "FieldError" will be
Expand Down
41 changes: 41 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9201,3 +9201,44 @@ func TestDatetimeValidation(t *testing.T) {
_ = validate.Var(2, "datetime")
}, "Bad field type int")
}

func TestTimeZoneValidation(t *testing.T) {
tests := []struct {
value string `validate:"timezone"`
tag string
expected bool
}{
// systems may have different time zone database, some systems time zone are case insensitive
{"America/New_York", `timezone`, true},
{"UTC", `timezone`, true},
{"", `timezone`, false},
{"Local", `timezone`, false},
{"Unknown", `timezone`, 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 time zone failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d time zone failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "timezone" {
t.Fatalf("Index: %d time zone failed Error: %s", i, errs)
}
}
}
}

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