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 'FloatAtLeast' and 'FloatAtMost' validation functions #239

Merged
merged 1 commit into from
Nov 7, 2019
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
38 changes: 38 additions & 0 deletions helper/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,44 @@ func FloatBetween(min, max float64) schema.SchemaValidateFunc {
}
}

// FloatAtLeast returns a SchemaValidateFunc which tests if the provided value
// is of type float and is at least min (inclusive)
func FloatAtLeast(min float64) schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(float64)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be float", k))
return
}

if v < min {
es = append(es, fmt.Errorf("expected %s to be at least (%f), got %f", k, min, v))
return
}

return
}
}

// FloatAtMost returns a SchemaValidateFunc which tests if the provided value
// is of type float and is at most max (inclusive)
func FloatAtMost(max float64) schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(float64)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be float", k))
return
}

if v > max {
es = append(es, fmt.Errorf("expected %s to be at most (%f), got %f", k, max, v))
return
}

return
}
}

// StringDoesNotContainAny returns a SchemaValidateFunc which validates that the
// provided value does not contain any of the specified Unicode code points in chars.
func StringDoesNotContainAny(chars string) schema.SchemaValidateFunc {
Expand Down
106 changes: 76 additions & 30 deletions helper/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,36 +427,7 @@ func TestValidationNoZeroValues(t *testing.T) {
})
}

func runTestCases(t *testing.T, cases []testCase) {
matchErr := func(errs []error, r *regexp.Regexp) bool {
// err must match one provided
for _, err := range errs {
if r.MatchString(err.Error()) {
return true
}
}

return false
}

for i, tc := range cases {
_, errs := tc.f(tc.val, "test_property")

if len(errs) == 0 && tc.expectedErr == nil {
continue
}

if len(errs) != 0 && tc.expectedErr == nil {
t.Fatalf("expected test case %d to produce no errors, got %v", i, errs)
}

if !matchErr(errs, tc.expectedErr) {
t.Fatalf("expected test case %d to produce error matching \"%s\", got %v", i, tc.expectedErr, errs)
}
}
}

func TestFloatBetween(t *testing.T) {
func TestValidateFloatBetween(t *testing.T) {
Copy link
Contributor

@appilon appilon Nov 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol it seems we've been a bit inconsistent, the integer equivalents are named TestValidationIntAtMost.

cases := map[string]struct {
Value interface{}
ValidateFunc schema.SchemaValidateFunc
Expand Down Expand Up @@ -499,6 +470,52 @@ func TestFloatBetween(t *testing.T) {
}
}

func TestValidateFloatAtLeast(t *testing.T) {
runTestCases(t, []testCase{
{
val: 2.5,
f: FloatAtLeast(1.5),
},
{
val: -1.0,
f: FloatAtLeast(-1.5),
},
{
val: 1.5,
f: FloatAtLeast(2.5),
expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(2\\.5\\d*\\), got 1\\.5\\d*"),
},
{
val: "2.5",
f: FloatAtLeast(1.5),
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be float"),
},
})
}

func TestValidateFloatAtMost(t *testing.T) {
runTestCases(t, []testCase{
{
val: 2.5,
f: FloatAtMost(3.5),
},
{
val: -1.0,
f: FloatAtMost(-0.5),
},
{
val: 2.5,
f: FloatAtMost(1.5),
expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(1\\.5\\d*\\), got 2\\.5\\d*"),
},
{
val: "2.5",
f: FloatAtMost(3.5),
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be float"),
},
})
}

func TestStringDoesNotContainAny(t *testing.T) {
chars := "|:/"

Expand Down Expand Up @@ -526,3 +543,32 @@ func TestStringDoesNotContainAny(t *testing.T) {
}
}
}

func runTestCases(t *testing.T, cases []testCase) {
matchErr := func(errs []error, r *regexp.Regexp) bool {
// err must match one provided
for _, err := range errs {
if r.MatchString(err.Error()) {
return true
}
}

return false
}

for i, tc := range cases {
_, errs := tc.f(tc.val, "test_property")

if len(errs) == 0 && tc.expectedErr == nil {
continue
}

if len(errs) != 0 && tc.expectedErr == nil {
t.Fatalf("expected test case %d to produce no errors, got %v", i, errs)
}

if !matchErr(errs, tc.expectedErr) {
t.Fatalf("expected test case %d to produce error matching \"%s\", got %v", i, tc.expectedErr, errs)
}
}
}