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

validation: add additional Int, Network, Time and Web validators #296

Merged
merged 15 commits into from
Jan 16, 2020

Conversation

katbyte
Copy link
Contributor

@katbyte katbyte commented Jan 15, 2020

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

@katbyte katbyte changed the title validation: add Int, Network, Time and Web validators validation: add additional Int, Network, Time and Web validators Jan 15, 2020
Copy link
Member

@radeksimko radeksimko left a 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.

@katbyte
Copy link
Contributor Author

katbyte commented Jan 15, 2020

Happy to make those changes @radeksimko, however SingleIP, IPRange already existed in the SDK. Once this is merged i will be opening a PR that deprecates and noralizes the remaining validators that match 🙂

@radeksimko
Copy link
Member

I see, apologies - it wasn't clear from the diff 🙃

@katbyte
Copy link
Contributor Author

katbyte commented Jan 15, 2020

Renamed! & ready for review

@katbyte
Copy link
Contributor Author

katbyte commented Jan 15, 2020

I will also say i am torn with functions like StringIsRFC3339Time and StringIsDayOfWeek, i almost want to just have them be IsDayOfWeek, IsRFC3339Time? but not sure if that is a good idea or not.

@appilon
Copy link
Contributor

appilon commented Jan 15, 2020

@katbyte I personally prefer the String prefix, to me it implies first and foremost we will typecheck and error if the type was not a string.

return
}

if v < 0 || 65535 < v {
Copy link
Contributor

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 { ... }

@@ -83,3 +103,23 @@ func IntInSlice(valid []int) schema.SchemaValidateFunc {
return
}
}

// IntInSlice returns a SchemaValidateFunc which tests if the provided value
Copy link
Contributor

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) {
Copy link
Contributor

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) {
Copy link
Contributor

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) {
Copy link
Contributor

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 {
Copy link
Contributor

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 {
Copy link
Contributor

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) {
Copy link
Contributor

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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Needs doc/comment

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func URLIsHTTPS(i interface{}, k string) (_ []string, errors []error) {
Copy link
Contributor

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

"testing"
)

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

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

"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
Copy link
Contributor

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

@appilon appilon merged commit 5cc4681 into master Jan 16, 2020
@appilon appilon deleted the kt/azurerm-validators-2 branch January 16, 2020 17:33
@ghost
Copy link

ghost commented Feb 16, 2020

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.

@ghost ghost locked and limited conversation to collaborators Feb 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants