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

Provider function to replace validation.NoZeroValues #2467

Merged
merged 13 commits into from
Dec 7, 2018
4 changes: 2 additions & 2 deletions azurerm/data_source_route_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-04-01/network"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand All @@ -17,7 +17,7 @@ func dataSourceArmRouteTable() *schema.Resource {
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
ValidateFunc: validate.NoEmptyStrings(),
WodansSon marked this conversation as resolved.
Show resolved Hide resolved
},

"resource_group_name": resourceGroupNameForDataSourceSchema(),
Expand Down
26 changes: 26 additions & 0 deletions azurerm/helpers/validate/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package validate

import (
"fmt"
"strings"

"github.com/hashicorp/terraform/helper/schema"
)

// NoEmptyStrings validates that the string is not just whitespace characters (equal to [\r\n\t\f\v ])
func NoEmptyStrings() schema.SchemaValidateFunc {
return func(i interface{}, k string) ([]string, []error) {
v, ok := i.(string)
if !ok {
return nil, []error{fmt.Errorf("expected type of %q to be string", k)}
}

newv := strings.TrimSpace(v)

if len(newv) == 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this could be:

if strings.TrimSpace(v) == "" {

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep... doesn't make sense to spin up a new var when you can do it inline.

return nil, []error{fmt.Errorf("%q must not be empty", k)}
}

return nil, nil
}
}
67 changes: 67 additions & 0 deletions azurerm/helpers/validate/strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package validate

import "testing"

func TestNoEmptyStrings(t *testing.T) {
cases := []struct {
Value string
TestName string
ErrCount int
}{
{
Value: "!",
TestName: "Exclamation",
ErrCount: 0,
},
{
Value: ".",
TestName: "Period",
ErrCount: 0,
},
{
Value: "-",
TestName: "Hyphen",
ErrCount: 0,
},
{
Value: "_",
TestName: "Underscore",
ErrCount: 0,
},
{
Value: "10.1.0.0/16",
TestName: "IP",
ErrCount: 0,
},
{
Value: "",
TestName: "Empty",
ErrCount: 1,
},
{
Value: " ",
TestName: "Space",
ErrCount: 1,
},
{
Value: " 1",
TestName: "DoubleSpaceOne",
ErrCount: 0,
},
{
Value: "1 ",
TestName: "OneSpace",
ErrCount: 0,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

it'd be good to add a test case for 5 empty spaces in here too

Copy link
Collaborator Author

@WodansSon WodansSon Dec 7, 2018

Choose a reason for hiding this comment

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

Done.

}

for _, tc := range cases {
t.Run(tc.TestName, func(t *testing.T) {
_, errors := NoEmptyStrings()(tc.Value, tc.TestName)

if len(errors) < tc.ErrCount {
t.Fatalf("Expected NoEmptyStrings to have %d not %d errors for %q", tc.ErrCount, len(errors), tc.TestName)
}
})
}
}
9 changes: 5 additions & 4 deletions azurerm/resource_arm_route_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand All @@ -30,7 +31,7 @@ func resourceArmRouteTable() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
ValidateFunc: validate.NoEmptyStrings(),
WodansSon marked this conversation as resolved.
Show resolved Hide resolved
},

"location": locationSchema(),
Expand All @@ -46,13 +47,13 @@ func resourceArmRouteTable() *schema.Resource {
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
ValidateFunc: validate.NoEmptyStrings(),
WodansSon marked this conversation as resolved.
Show resolved Hide resolved
},

"address_prefix": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
ValidateFunc: validate.NoEmptyStrings(),
WodansSon marked this conversation as resolved.
Show resolved Hide resolved
},

"next_hop_type": {
Expand All @@ -71,7 +72,7 @@ func resourceArmRouteTable() *schema.Resource {
"next_hop_in_ip_address": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.NoZeroValues,
ValidateFunc: validate.NoEmptyStrings(),
WodansSon marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
Expand Down