Skip to content

Commit

Permalink
standardized validation function sigs
Browse files Browse the repository at this point in the history
  • Loading branch information
katbyte committed Nov 21, 2018
1 parent 65b7153 commit c179a31
Show file tree
Hide file tree
Showing 41 changed files with 196 additions and 193 deletions.
22 changes: 11 additions & 11 deletions azurerm/helpers/azure/key_vault_child.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,34 @@ func ParseKeyVaultChildID(id string) (*KeyVaultChildID, error) {
return &childId, nil
}

func ValidateKeyVaultChildName(v interface{}, k string) (ws []string, es []error) {
func ValidateKeyVaultChildName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

if matched := regexp.MustCompile(`^[0-9a-zA-Z-]+$`).Match([]byte(value)); !matched {
es = append(es, fmt.Errorf("%q may only contain alphanumeric characters and dashes", k))
errors = append(errors, fmt.Errorf("%q may only contain alphanumeric characters and dashes", k))
}

return ws, es
return warnings, errors
}

// Unfortunately this can't (easily) go in the Validate package
// since there's a circular reference on this package
func ValidateKeyVaultChildId(i interface{}, k string) (s []string, es []error) {
if s, es = validation.NoZeroValues(i, k); len(es) > 0 {
return s, es
func ValidateKeyVaultChildId(i interface{}, k string) (warnings []string, errors []error) {
if warnings, errors = validation.NoZeroValues(i, k); len(errors) > 0 {
return warnings, errors
}

v, ok := i.(string)
if !ok {
es = append(es, fmt.Errorf("Expected %s to be a string!", k))
return s, es
errors = append(errors, fmt.Errorf("Expected %s to be a string!", k))
return warnings, errors
}

_, err := ParseKeyVaultChildID(v)
if err != nil {
es = append(es, fmt.Errorf("Error parsing Key Vault Child ID: %s", err))
return s, es
errors = append(errors, fmt.Errorf("Error parsing Key Vault Child ID: %s", err))
return warnings, errors
}

return s, es
return warnings, errors
}
10 changes: 5 additions & 5 deletions azurerm/helpers/azure/resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ func SchemaResourceGroupNameForDataSource() *schema.Schema {
}
}

func validateResourceGroupName(v interface{}, k string) (ws []string, es []error) {
func validateResourceGroupName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

if len(value) > 80 {
es = append(es, fmt.Errorf("%q may not exceed 80 characters in length", k))
errors = append(errors, fmt.Errorf("%q may not exceed 80 characters in length", k))
}

if strings.HasSuffix(value, ".") {
es = append(es, fmt.Errorf("%q may not end with a period", k))
errors = append(errors, fmt.Errorf("%q may not end with a period", k))
}

// regex pulled from https://docs.microsoft.com/en-us/rest/api/resources/resourcegroups/createorupdate
if matched := regexp.MustCompile(`^[-\w\._\(\)]+$`).Match([]byte(value)); !matched {
es = append(es, fmt.Errorf("%q may only contain alphanumeric characters, dash, underscores, parentheses and periods", k))
errors = append(errors, fmt.Errorf("%q may only contain alphanumeric characters, dash, underscores, parentheses and periods", k))
}

return ws, es
return warnings, errors
}
4 changes: 2 additions & 2 deletions azurerm/helpers/azure/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"regexp"
)

func ValidateResourceID(i interface{}, k string) (ws []string, errors []error) {
func ValidateResourceID(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
Expand All @@ -16,7 +16,7 @@ func ValidateResourceID(i interface{}, k string) (ws []string, errors []error) {
errors = append(errors, fmt.Errorf("Can not parse %q as a resource id: %v", k, err))
}

return ws, errors
return warnings, errors
}

//true for a resource ID or an empty string
Expand Down
18 changes: 9 additions & 9 deletions azurerm/helpers/validate/api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@ import (
"regexp"
)

func ApiManagementServiceName(v interface{}, k string) (ws []string, es []error) {
func ApiManagementServiceName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

if matched := regexp.MustCompile(`^[0-9a-zA-Z-]{1,50}$`).Match([]byte(value)); !matched {
es = append(es, fmt.Errorf("%q may only contain alphanumeric characters and dashes up to 50 characters in length", k))
errors = append(errors, fmt.Errorf("%q may only contain alphanumeric characters and dashes up to 50 characters in length", k))
}

return ws, es
return warnings, errors
}

func ApiManagementServicePublisherName(v interface{}, k string) (ws []string, es []error) {
func ApiManagementServicePublisherName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

if matched := regexp.MustCompile(`^.{1,100}$`).Match([]byte(value)); !matched {
es = append(es, fmt.Errorf("%q may only be up to 100 characters in length", k))
errors = append(errors, fmt.Errorf("%q may only be up to 100 characters in length", k))
}

return ws, es
return warnings, errors
}

func ApiManagementServicePublisherEmail(v interface{}, k string) (ws []string, es []error) {
func ApiManagementServicePublisherEmail(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

if matched := regexp.MustCompile(`^[\S*]{1,100}$`).Match([]byte(value)); !matched {
es = append(es, fmt.Errorf("%q may only be up to 100 characters in length", k))
errors = append(errors, fmt.Errorf("%q may only be up to 100 characters in length", k))
}

return ws, es
return warnings, errors
}
8 changes: 4 additions & 4 deletions azurerm/helpers/validate/base64.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ import (
)

func Base64String() schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
return func(i interface{}, k string) (warnings []string, errors []error) {
// Empty string is not allowed
if s, es = validation.NoZeroValues(i, k); len(es) > 0 {
if warnings, errors = validation.NoZeroValues(i, k); len(errors) > 0 {
return
}

v, ok := i.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
return
}

if _, err := base64.StdEncoding.DecodeString(v); err != nil {
es = append(es, fmt.Errorf("expect value (%s) of %s is base64 string", v, k))
errors = append(errors, fmt.Errorf("expect value (%s) of %s is base64 string", v, k))
}

return
Expand Down
22 changes: 11 additions & 11 deletions azurerm/helpers/validate/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,46 @@ import (
"regexp"
)

func SharedImageGalleryName(v interface{}, k string) (ws []string, es []error) {
func SharedImageGalleryName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)
// Image gallery name accepts only alphanumeric, dots and underscores in the name (no dashes)
r, _ := regexp.Compile(`^[A-Za-z0-9._]+$`)
if !r.MatchString(value) {
es = append(es, fmt.Errorf("%s can only contain alphanumeric, full stops and underscores. Got %q.", k, value))
errors = append(errors, fmt.Errorf("%s can only contain alphanumeric, full stops and underscores. Got %q.", k, value))
}

length := len(value)
if length >= 80 {
es = append(es, fmt.Errorf("%s can be up to 80 characters, currently %d.", k, length))
errors = append(errors, fmt.Errorf("%s can be up to 80 characters, currently %d.", k, length))
}

return ws, es
return warnings, errors
}

func SharedImageName(v interface{}, k string) (ws []string, es []error) {
func SharedImageName(v interface{}, k string) (warnings []string, errors []error) {
// different from the shared image gallery name
value := v.(string)

r, _ := regexp.Compile(`^[A-Za-z0-9._-]+$`)
if !r.MatchString(value) {
es = append(es, fmt.Errorf("%s can only contain alphanumeric, full stops, dashes and underscores. Got %q.", k, value))
errors = append(errors, fmt.Errorf("%s can only contain alphanumeric, full stops, dashes and underscores. Got %q.", k, value))
}

length := len(value)
if length >= 80 {
es = append(es, fmt.Errorf("%s can be up to 80 characters, currently %d.", k, length))
errors = append(errors, fmt.Errorf("%s can be up to 80 characters, currently %d.", k, length))
}

return ws, es
return warnings, errors
}

func SharedImageVersionName(v interface{}, k string) (ws []string, es []error) {
func SharedImageVersionName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

r, _ := regexp.Compile(`^([0-9]{1,10}\.[0-9]{1,10}\.[0-9]{1,10})$`)
if !r.MatchString(value) {
es = append(es, fmt.Errorf("Expected %s to be in the format `1.2.3` but got %q.", k, value))
errors = append(errors, fmt.Errorf("Expected %s to be in the format `1.2.3` but got %q.", k, value))
}

return ws, es
return warnings, errors
}
12 changes: 6 additions & 6 deletions azurerm/helpers/validate/devspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import (
)

func DevSpaceName() schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
return func(i interface{}, k string) (warnings []string, errors []error) {
// Length should be between 3 and 31.
if s, es = validation.StringLenBetween(3, 31)(i, k); len(es) > 0 {
return s, es
if warnings, errors = validation.StringLenBetween(3, 31)(i, k); len(errors) > 0 {
return warnings, errors
}

// Naming rule.
regexStr := "^[a-zA-Z0-9](-?[a-zA-Z0-9])*$"
errMsg := "DevSpace name can only include alphanumeric characters, hyphens."
if s, es = validation.StringMatch(regexp.MustCompile(regexStr), errMsg)(i, k); len(es) > 0 {
return s, es
if warnings, errors = validation.StringMatch(regexp.MustCompile(regexStr), errMsg)(i, k); len(errors) > 0 {
return warnings, errors
}

return s, es
return warnings, errors
}
}
26 changes: 13 additions & 13 deletions azurerm/helpers/validate/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,53 @@ import (
// IntBetweenAndDivisibleBy returns a SchemaValidateFunc which tests if the provided value
// is of type int and is between min and max (inclusive) and is divisible by a given number
func IntBetweenAndDivisibleBy(min, max, divisor int) schema.SchemaValidateFunc { // nolint: unparam
return func(i interface{}, k string) (s []string, es []error) {
return func(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(int)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be int", k))
errors = append(errors, 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))
errors = append(errors, fmt.Errorf("expected %s to be in the range (%d - %d), got %d", k, min, max, v))
return
}

if math.Mod(float64(v), float64(divisor)) != 0 {
es = append(es, fmt.Errorf("expected %s to be divisible by %d", k, divisor))
errors = append(errors, fmt.Errorf("expected %s to be divisible by %d", k, divisor))
return
}

return s, es
return warnings, errors
}
}

// 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 { // nolint: unparam
return func(i interface{}, k string) (s []string, es []error) {
return func(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(int)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be int", k))
errors = append(errors, fmt.Errorf("expected type of %s to be int", k))
return
}

if math.Mod(float64(v), float64(divisor)) != 0 {
es = append(es, fmt.Errorf("expected %s to be divisible by %d", k, divisor))
errors = append(errors, fmt.Errorf("expected %s to be divisible by %d", k, divisor))
return
}

return s, es
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) {
return func(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(int)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be int", k))
errors = append(errors, fmt.Errorf("expected type of %s to be int", k))
return
}

Expand All @@ -66,7 +66,7 @@ func IntInSlice(valid []int) schema.SchemaValidateFunc {
}
}

es = append(es, fmt.Errorf("expected %q to be one of %v, got %v", k, valid, v))
return s, es
errors = append(errors, fmt.Errorf("expected %q to be one of %v, got %v", k, valid, v))
return warnings, errors
}
}
12 changes: 6 additions & 6 deletions azurerm/helpers/validate/iothub.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import (
"regexp"
)

func IoTHubName(v interface{}, k string) (ws []string, es []error) {
func IoTHubName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

// Portal: The value must contain only alphanumeric characters or the following: -
if matched := regexp.MustCompile(`^[0-9a-zA-Z-]{1,}$`).Match([]byte(value)); !matched {
es = append(es, fmt.Errorf("%q may only contain alphanumeric characters and dashes", k))
errors = append(errors, fmt.Errorf("%q may only contain alphanumeric characters and dashes", k))
}

return ws, es
return warnings, errors
}

func IoTHubConsumerGroupName(v interface{}, k string) (ws []string, es []error) {
func IoTHubConsumerGroupName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

// Portal: The value must contain only alphanumeric characters or the following: - . _
if matched := regexp.MustCompile(`^[0-9a-zA-Z-._]{1,}$`).Match([]byte(value)); !matched {
es = append(es, fmt.Errorf("%q may only contain alphanumeric characters and dashes, periods and underscores", k))
errors = append(errors, fmt.Errorf("%q may only contain alphanumeric characters and dashes, periods and underscores", k))
}

return ws, es
return warnings, errors
}
4 changes: 2 additions & 2 deletions azurerm/helpers/validate/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func IPv6Address(i interface{}, k string) (warnings []string, errors []error) {
return validateIpv6Address(i, k, false)
}

func validateIpv6Address(i interface{}, k string, allowEmpty bool) (ws []string, errors []error) { // nolint: unparam
func validateIpv6Address(i interface{}, k string, allowEmpty bool) (warnings []string, errors []error) { // nolint: unparam
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
Expand All @@ -25,7 +25,7 @@ func validateIpv6Address(i interface{}, k string, allowEmpty bool) (ws []string,
errors = append(errors, fmt.Errorf("%q is not a valid IPv6 address: %q", k, v))
}

return ws, errors
return warnings, errors

}

Expand Down
4 changes: 2 additions & 2 deletions azurerm/helpers/validate/public_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"regexp"
)

func PublicIpDomainNameLabel(v interface{}, k string) (ws []string, errors []error) {
func PublicIpDomainNameLabel(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[a-z][a-z0-9-]{1,61}[a-z0-9]$`).MatchString(value) {
errors = append(errors, fmt.Errorf("%s must contain only lowercase alphanumeric characters, numbers and hyphens. It must start with a letter and end only with a number or letter", k))
}
return ws, errors
return warnings, errors
}
4 changes: 2 additions & 2 deletions azurerm/helpers/validate/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/hashicorp/go-uuid"
)

func UUID(i interface{}, k string) (ws []string, errors []error) {
func UUID(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
Expand All @@ -17,5 +17,5 @@ func UUID(i interface{}, k string) (ws []string, errors []error) {
errors = append(errors, fmt.Errorf("%q isn't a valid UUID (%q): %+v", k, v, err))
}

return ws, errors
return warnings, errors
}
Loading

0 comments on commit c179a31

Please sign in to comment.