-
Notifications
You must be signed in to change notification settings - Fork 233
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
Conversation
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.
I will let you rebase this before fully reviewing, but I thought it would be good to bring this up early.
I think ideally the function names should communicate whether it's a positive or negative validation. I understand that negated validation doesn't make sense in all cases, but it would be great to have some consistency and clarity.
for most, or at least these I'd consider using Is
, Valid
, or IsValid
prefix
- IPv6Address
- IPv4Address
- CIDR
- MACAddress
- PortNumber
- PortNumberOrZero
- URLWithScheme
Also it would be great to list all the validation functions being added, so we can paste these into the Changelog - I noticed there's a few more (e.g. SingleIP
, IPRange
).
FYI: I also created #298 to spin off naming convention as a parallel topic.
Happy to make those changes @radeksimko, however |
I see, apologies - it wasn't clear from the diff 🙃 |
Renamed! & ready for review |
I will also say i am torn with functions like |
@katbyte I personally prefer the |
helper/validation/network.go
Outdated
return | ||
} | ||
|
||
if v < 0 || 65535 < v { |
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.
nitpick, prefer the variable to always be on the left side of the comparison
if v < 0 || v > 65535 { ... }
helper/validation/int.go
Outdated
@@ -83,3 +103,23 @@ func IntInSlice(valid []int) schema.SchemaValidateFunc { | |||
return | |||
} | |||
} | |||
|
|||
// IntInSlice returns a SchemaValidateFunc which tests if the provided value |
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
"expected %s to contain a valid network CIDR, expected %s, got %s", | ||
k, ipnet, v)) | ||
} | ||
func IsIPv6Address(i interface{}, k string) (warnings []string, errors []error) { |
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.
Needs doc/comment
return warnings, errors | ||
} | ||
|
||
func IsIPv4Address(i interface{}, k string) (warnings []string, errors []error) { |
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.
Needs doc/comment
// IPRange returns a SchemaValidateFunc which tests if the provided value | ||
// is of type string, and in valid IP range notation | ||
func IPRange() schema.SchemaValidateFunc { | ||
func IsCIDR(i interface{}, k string) (warnings []string, errors []error) { |
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.
Needs doc/comment
func ValidateRFC3339TimeString(v interface{}, k string) (ws []string, errors []error) { | ||
if _, err := time.Parse(time.RFC3339, v.(string)); err != nil { | ||
errors = append(errors, fmt.Errorf("%q: invalid RFC3339 timestamp", k)) | ||
func IsDayOfTheWeek(ignoreCase bool) schema.SchemaValidateFunc { |
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.
Needs doc/comment
}, ignoreCase) | ||
} | ||
|
||
func IsMonth(ignoreCase bool) schema.SchemaValidateFunc { |
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.
Needs doc/comment
} | ||
|
||
// ValidateRFC3339TimeString is a ValidateFunc that ensures a string parses as time.RFC3339 format | ||
func IsRFC3339Time(i interface{}, k string) (warnings []string, errors []error) { |
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 needs update
return IsURLWithScheme([]string{"http", "https"})(i, k) | ||
} | ||
|
||
func IsURLWithScheme(validSchemes []string) schema.SchemaValidateFunc { |
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.
Needs doc/comment
helper/validation/web.go
Outdated
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func URLIsHTTPS(i interface{}, k string) (_ []string, errors []error) { |
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.
I like the way this flows... but it feels inconsistent, anyway this can be reworded to being with Is
... maybe
IsHTTPSURL
and
IsHTTPURL
helper/validation/web_test.go
Outdated
"testing" | ||
) | ||
|
||
func TestURLIsHTTPS(t *testing.T) { |
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.
just need to update Test names
helper/validation/web.go
Outdated
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
// IsURLWithHTTPS is a SchemaValidateFunc which tests if the provided value is of type string and a valid IPv6 address |
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.
Ah dang looks like doc/comments need description update
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
branched off #295 so diff should clean up once its merged
New Validators:
IntDivisibleBy
IntNotInSlice
IsIPv6Address
IsIPv4Address
IsCIDR
IsMACAddress
IsPortNumber
IsPortNumberOrZero
IsDayOfTheWeek
IsMonth
IsRFC3339Time
IsURLWithHTTPS
IsURLWithHTTPorHTTPS
IsURLWithScheme