-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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 name validation function, standardise validation functions used #164
Conversation
…alidation function.
google/validation.go
Outdated
"regexp" | ||
) | ||
|
||
func validateGCPName(v interface{}, k string) (ws []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.
Good candidate for unit testing here
Also what do you think about splitting it out into two functions - validateGCPName
and validateRegexp
(where validateGCPName
is implemented in terms of validateRegexp
)? I can see us needing regexs for other things later (for example, metadata keys)
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.
Done.
@@ -68,7 +69,7 @@ func dataSourceGoogleSignedUrl() *schema.Resource { | |||
Type: schema.TypeString, | |||
Optional: true, | |||
Default: "GET", | |||
ValidateFunc: validateHttpMethod, | |||
ValidateFunc: validation.StringInSlice([]string{"GET", "HEAD", "PUT", "DELETE"}, true), |
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.
Could add our own validation function that is 'isHTTPVerb' (although given not all the http verbs are here, maybe that would be more confusing?) What do you think?
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.
Given that not all of the http verbs are here, I don't think we should add a custom validator yet; if there are several other validators with identical parameters in the future, that is something we should consider, though.
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.
SGTM
"regexp" | ||
) | ||
|
||
func validateGCPName(v interface{}, k string) (ws []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.
Shouldn't this also return schema.SchemaValidationFunc?
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.
Or, better suggestion - make the other function return (ws []string, errors []error) (not a func)
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.
validateGCPName
is implicitly a schema.SchemaValidationFunc
because of its signature; we want validateRegexp
to validate a against a variable regex and not a fixed one like validateGCPName
, so we pass that regex into a function that makes a schema.SchemaValidationFunc
for us.
There are similar functions to validateRegexp
living in the core Terraform repo's helper/validation package.
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.
Spoke in person; I understand the differing signatures here. I'm fine with the code as is.
…ashicorp#164) * Used standard validation functions where possible, added a GCP name validation function. * Add tests for GCP name, factor out a ValidateRegexp function. * make fmt
…_check (hashicorp#164) <!-- This change is generated by MagicModules. --> /cc @rileykarson
…_check (hashicorp#164) <!-- This change is generated by MagicModules. --> /cc @rileykarson
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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks! |
We have several validation functions that are just reimplementations of ones that live in core's
helper/validation
package; let's use those instead.We define a separate validation function each time we want to validate the
name
field of a resource. Let's use a single one instead.These two resources had validation functions for
name
that validated only length and not format, but both follow the same rules as most names, a 1-63 character RFC1035 string. Errors will now surface at plan time instead of apply time for them.