-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provider function to replace validation.NoZeroValues (#2467)
* Fix for name with only spaces * Removed regex and replaced with TrimSpace * Updated test for new behavior * Saved as LF only * Fix lint error * Update azurerm/data_source_route_table.go Co-Authored-By: jeffreyCline <[email protected]> * Update azurerm/resource_arm_route_table.go Co-Authored-By: jeffreyCline <[email protected]> * Update azurerm/resource_arm_route_table.go Co-Authored-By: jeffreyCline <[email protected]> * Update azurerm/resource_arm_route_table.go Co-Authored-By: jeffreyCline <[email protected]> * Update azurerm/resource_arm_route_table.go Co-Authored-By: jeffreyCline <[email protected]> * Update signature * Added test case * Added special character test cases * Permission to merge per KT
- Loading branch information
Showing
4 changed files
with
126 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
{ | ||
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters