-
Notifications
You must be signed in to change notification settings - Fork 230
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
validation: add additional Int, Network, Time and Web validators #296
Merged
Merged
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3f3dde2
sort validation function into separate files
katbyte 9a86338
cleanup
katbyte ee4fe2a
make fmt
katbyte c1005d6
validation: add Int, Network, Time and Web validators
katbyte af70e5e
Fix errant find replace
katbyte 197bcb9
git merge master -S
katbyte 2504e75
Added Is to function names
katbyte 43d0e8f
Removed String from network and time functions
katbyte d00d319
Address PR comments
katbyte c4e0f07
make fmt
katbyte 03bfef2
Further PR comments
katbyte f86cf8d
adjust max port value
katbyte e481cb9
rename tests
katbyte 4770c6a
Update web.go
katbyte cc4d279
Update web.go
katbyte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,64 @@ | ||
package validation | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
// FloatBetween returns a SchemaValidateFunc which tests if the provided value | ||
// is of type float64 and is between min and max (inclusive). | ||
func FloatBetween(min, 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 float64", k)) | ||
return | ||
} | ||
|
||
if v < min || v > max { | ||
es = append(es, fmt.Errorf("expected %s to be in the range (%f - %f), got %f", k, min, max, v)) | ||
return | ||
} | ||
|
||
return | ||
} | ||
} | ||
|
||
// 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 | ||
} | ||
} |
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,97 @@ | ||
package validation | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func TestValidateFloatBetween(t *testing.T) { | ||
cases := map[string]struct { | ||
Value interface{} | ||
ValidateFunc schema.SchemaValidateFunc | ||
ExpectValidationErrors bool | ||
}{ | ||
"accept valid value": { | ||
Value: 1.5, | ||
ValidateFunc: FloatBetween(1.0, 2.0), | ||
ExpectValidationErrors: false, | ||
}, | ||
"accept valid value inclusive upper bound": { | ||
Value: 1.0, | ||
ValidateFunc: FloatBetween(0.0, 1.0), | ||
ExpectValidationErrors: false, | ||
}, | ||
"accept valid value inclusive lower bound": { | ||
Value: 0.0, | ||
ValidateFunc: FloatBetween(0.0, 1.0), | ||
ExpectValidationErrors: false, | ||
}, | ||
"reject out of range value": { | ||
Value: -1.0, | ||
ValidateFunc: FloatBetween(0.0, 1.0), | ||
ExpectValidationErrors: true, | ||
}, | ||
"reject incorrectly typed value": { | ||
Value: 1, | ||
ValidateFunc: FloatBetween(0.0, 1.0), | ||
ExpectValidationErrors: true, | ||
}, | ||
} | ||
|
||
for tn, tc := range cases { | ||
_, errors := tc.ValidateFunc(tc.Value, tn) | ||
if len(errors) > 0 && !tc.ExpectValidationErrors { | ||
t.Errorf("%s: unexpected errors %s", tn, errors) | ||
} else if len(errors) == 0 && tc.ExpectValidationErrors { | ||
t.Errorf("%s: expected errors but got none", tn) | ||
} | ||
} | ||
} | ||
|
||
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"), | ||
}, | ||
}) | ||
} |
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,125 @@ | ||
package validation | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
// IntBetween returns a SchemaValidateFunc which tests if the provided value | ||
// is of type int and is between min and max (inclusive) | ||
func IntBetween(min, max int) schema.SchemaValidateFunc { | ||
return func(i interface{}, k string) (s []string, es []error) { | ||
v, ok := i.(int) | ||
if !ok { | ||
es = append(es, fmt.Errorf("expected type of %s to be int", k)) | ||
return | ||
} | ||
|
||
if v < min || v > max { | ||
es = append(es, fmt.Errorf("expected %s to be in the range (%d - %d), got %d", k, min, max, v)) | ||
return | ||
} | ||
|
||
return | ||
} | ||
} | ||
|
||
// IntAtLeast returns a SchemaValidateFunc which tests if the provided value | ||
// is of type int and is at least min (inclusive) | ||
func IntAtLeast(min int) schema.SchemaValidateFunc { | ||
return func(i interface{}, k string) (s []string, es []error) { | ||
v, ok := i.(int) | ||
if !ok { | ||
es = append(es, fmt.Errorf("expected type of %s to be int", k)) | ||
return | ||
} | ||
|
||
if v < min { | ||
es = append(es, fmt.Errorf("expected %s to be at least (%d), got %d", k, min, v)) | ||
return | ||
} | ||
|
||
return | ||
} | ||
} | ||
|
||
// IntAtMost returns a SchemaValidateFunc which tests if the provided value | ||
// is of type int and is at most max (inclusive) | ||
func IntAtMost(max int) schema.SchemaValidateFunc { | ||
return func(i interface{}, k string) (s []string, es []error) { | ||
v, ok := i.(int) | ||
if !ok { | ||
es = append(es, fmt.Errorf("expected type of %s to be int", k)) | ||
return | ||
} | ||
|
||
if v > max { | ||
es = append(es, fmt.Errorf("expected %s to be at most (%d), got %d", k, max, v)) | ||
return | ||
} | ||
|
||
return | ||
} | ||
} | ||
|
||
// IntDivisibleBy returns a SchemaValidateFunc which tests if the provided value | ||
// is of type int and is divisible by a given number | ||
func IntDivisibleBy(divisor int) schema.SchemaValidateFunc { | ||
return func(i interface{}, k string) (warnings []string, errors []error) { | ||
v, ok := i.(int) | ||
if !ok { | ||
errors = append(errors, fmt.Errorf("expected type of %s to be int", k)) | ||
return | ||
} | ||
|
||
if math.Mod(float64(v), float64(divisor)) != 0 { | ||
errors = append(errors, fmt.Errorf("expected %s to be divisible by %d, got: %v", k, divisor, i)) | ||
return | ||
} | ||
|
||
return warnings, errors | ||
} | ||
} | ||
|
||
// IntInSlice returns a SchemaValidateFunc which tests if the provided value | ||
// is of type int and matches the value of an element in the valid slice | ||
func IntInSlice(valid []int) schema.SchemaValidateFunc { | ||
return func(i interface{}, k string) (s []string, es []error) { | ||
v, ok := i.(int) | ||
if !ok { | ||
es = append(es, fmt.Errorf("expected type of %s to be an integer", k)) | ||
return | ||
} | ||
|
||
for _, validInt := range valid { | ||
if v == validInt { | ||
return | ||
} | ||
} | ||
|
||
es = append(es, fmt.Errorf("expected %s to be one of %v, got %d", k, valid, v)) | ||
return | ||
} | ||
} | ||
|
||
// IntInSlice returns a SchemaValidateFunc which tests if the provided value | ||
// is of type int and matches the value of an element in the valid slice | ||
func IntNotInSlice(valid []int) schema.SchemaValidateFunc { | ||
return func(i interface{}, k string) (_ []string, errors []error) { | ||
v, ok := i.(int) | ||
if !ok { | ||
errors = append(errors, fmt.Errorf("expected type of %s to be an integer", k)) | ||
return | ||
} | ||
|
||
for _, validInt := range valid { | ||
if v == validInt { | ||
errors = append(errors, fmt.Errorf("expected %s to not be one of %v, got %d", k, valid, v)) | ||
} | ||
} | ||
|
||
return | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment/doc needs updating