This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from ripienaar/7
(#7) Support validating Go time.Duration durations
- Loading branch information
Showing
4 changed files
with
112 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package duration | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"time" | ||
) | ||
|
||
// ValidateString validates that input is a valid duration | ||
func ValidateString(input string) (bool, error) { | ||
_, err := time.ParseDuration(input) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// ValidateStructField validates a struct field holds a valid duration | ||
func ValidateStructField(value reflect.Value, tag string) (bool, error) { | ||
if value.Kind() != reflect.String { | ||
return false, fmt.Errorf("only strings can be Duration validated") | ||
} | ||
|
||
return ValidateString(value.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package duration | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestFileContent(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Validator/Duration") | ||
} | ||
|
||
var _ = Describe("ValidateString", func() { | ||
It("Should match durations correctly", func() { | ||
ok, err := ValidateString("1s") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(ok).To(BeTrue()) | ||
|
||
ok, err = ValidateString("1h") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(ok).To(BeTrue()) | ||
|
||
ok, err = ValidateString("1w") | ||
Expect(err).To(MatchError("time: unknown unit w in duration 1w")) | ||
Expect(ok).To(BeFalse()) | ||
}) | ||
}) | ||
|
||
var _ = Describe("ValidateStructField", func() { | ||
type t struct { | ||
Interval string `validate:"duration"` | ||
} | ||
|
||
It("Should validate the struct correctly", func() { | ||
st := t{"1h"} | ||
|
||
val := reflect.ValueOf(st) | ||
valueField := val.FieldByName("Interval") | ||
typeField, _ := val.Type().FieldByName("Interval") | ||
|
||
ok, err := ValidateStructField(valueField, typeField.Tag.Get("validate")) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(ok).To(BeTrue()) | ||
|
||
st.Interval = "foo" | ||
valueField = reflect.ValueOf(st).FieldByName("Interval") | ||
ok, err = ValidateStructField(valueField, typeField.Tag.Get("validate")) | ||
Expect(err).To(MatchError("time: invalid duration foo")) | ||
Expect(ok).To(BeFalse()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters