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,
},

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

import (
"fmt"
"strings"
)

// NoEmptyStrings validates that the string is not just whitespace characters (equal to [\r\n\t\f\v ])
func NoEmptyStrings(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)}
}

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

return nil, nil
}
99 changes: 99 additions & 0 deletions azurerm/helpers/validate/strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
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: " ",
TestName: "FiveSpaces",
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.

{
Value: "\r",
TestName: "CarriageReturn",
ErrCount: 1,
},
{
Value: "\n",
TestName: "NewLine",
ErrCount: 1,
},
{
Value: "\t",
TestName: "HorizontalTab",
ErrCount: 1,
},
{
Value: "\f",
TestName: "FormFeed",
ErrCount: 1,
},
{
Value: "\v",
TestName: "VerticalTab",
ErrCount: 1,
},
}

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,
},

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

"address_prefix": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
ValidateFunc: validate.NoEmptyStrings,
},

"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,
},
},
},
Expand Down