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

Support new strings format time-zone-name #30

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion strings/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type EnumValidationError struct {
}

func (err EnumValidationError) Error() string {
return fmt.Sprintf("input value %d doesn't exist in %v", err.Input, err.Definition.Enum)
return fmt.Sprintf("input value %s doesn't exist in %v", err.Input, err.Definition.Enum)
}

func NewEnumValidator(def EnumValidatorDefinition) (EnumValidator, error) {
Expand Down
16 changes: 15 additions & 1 deletion strings/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"regexp"
"time"
)

var (
Expand Down Expand Up @@ -34,7 +35,7 @@ func (f FormatValidationError) Error() string {

func NewFormatValidator(definition FormatValidatorDefinition) (FormatValidator, error) {
switch definition.Format {
case "date-time", "email", "hostname", "uri", "password-0Aa":
case "date-time", "email", "hostname", "uri", "password-0Aa", "time-zone-name":
return FormatValidator{definition}, nil
}
return FormatValidator{}, FormatDefinitionInvalidFormatError
Expand Down Expand Up @@ -72,6 +73,12 @@ func (f FormatValidator) Validate(input string) error {
return nil
}
break
case "time-zone-name":
ok := isTimeZoneName(input)
if ok {
return nil
}
break
}
return &FormatValidationError{
f.definition,
Expand Down Expand Up @@ -148,3 +155,10 @@ func isPassword0Aa(pw string) bool {
}
return large && small && number
}

func isTimeZoneName(name string) bool {
if _, err := time.LoadLocation(name); err != nil {
return false
}
return true
}
34 changes: 34 additions & 0 deletions strings/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func TestNewFormatValidator(t *testing.T) {
Definition: strings.FormatValidatorDefinition{Format: "password-0Aa"},
Error: nil,
},
{
Message: "time-zone-name format",
Definition: strings.FormatValidatorDefinition{Format: "time-zone-name"},
Error: nil,
},
{
Message: "empty string",
Definition: strings.FormatValidatorDefinition{Format: ""},
Expand Down Expand Up @@ -459,4 +464,33 @@ func TestFormatValidator(t *testing.T) {
t.Errorf("expected %v, but actual %v", c.Expected, err)
}
}

definition = strings.FormatValidatorDefinition{
Format: "time-zone-name",
}
va, err = strings.NewFormatValidator(definition)
if err != nil {
t.Error(err.Error())
}

cases = []FormatValidationTestCase{
{
Input: "Asia/Tokyo",
Expected: nil,
},
{
Input: "Asia/NeoTokyo",
Expected: &strings.FormatValidationError{
Input: "Asia/NeoTokyo",
Definition: definition,
},
},
}

for _, c := range cases {
err := va.Validate(c.Input)
if !reflect.DeepEqual(err, c.Expected) {
t.Errorf("expected %v, but actual %v", c.Expected, err)
}
}
}