Skip to content

Commit

Permalink
update name validation func to not accept white spaces at the end
Browse files Browse the repository at this point in the history
  • Loading branch information
imjaroiswebdev committed Aug 21, 2023
1 parent 601fdba commit 3a6043b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
17 changes: 15 additions & 2 deletions pagerduty/resource_pagerduty_escalation_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func TestAccPagerDutyEscalationPolicy_Basic(t *testing.T) {
func TestAccPagerDutyEscalationPolicy_FormatValidation(t *testing.T) {
username := fmt.Sprintf("tf-%s", acctest.RandString(5))
email := fmt.Sprintf("%s@foo.test", username)
errMessageMatcher := "Name must not be blank, nor contain the characters.*, or any non-printable characters. White spaces at the end are also not allowed."

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -123,13 +124,25 @@ func TestAccPagerDutyEscalationPolicy_FormatValidation(t *testing.T) {
{
Config: testAccCheckPagerDutyEscalationPolicyConfig(username, email, ""),
PlanOnly: true,
ExpectError: regexp.MustCompile("Name can't be blank neither contain.*, nor non-printable characters."),
ExpectError: regexp.MustCompile(errMessageMatcher),
},
// Name with & in it
{
Config: testAccCheckPagerDutyEscalationPolicyConfig(username, email, "this name has an ampersand (&)"),
PlanOnly: true,
ExpectError: regexp.MustCompile("Name can't be blank neither contain.*, nor non-printable characters."),
ExpectError: regexp.MustCompile(errMessageMatcher),
},
// Name with one white space at the end
{
Config: testAccCheckPagerDutyEscalationPolicyConfig(username, email, "this name has a white space at the end "),
PlanOnly: true,
ExpectError: regexp.MustCompile(errMessageMatcher),
},
// Name with multiple white space at the end
{
Config: testAccCheckPagerDutyEscalationPolicyConfig(username, email, "this name has white spaces at the end "),
PlanOnly: true,
ExpectError: regexp.MustCompile(errMessageMatcher),
},
},
})
Expand Down
17 changes: 15 additions & 2 deletions pagerduty/resource_pagerduty_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func TestAccPagerDutyService_FormatValidation(t *testing.T) {
username := fmt.Sprintf("tf-%s", acctest.RandString(5))
email := fmt.Sprintf("%s@foo.test", username)
escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5))
errMessageMatcher := "Name must not be blank, nor contain the characters.*, or any non-printable characters. White spaces at the end are also not allowed."

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -144,13 +145,25 @@ func TestAccPagerDutyService_FormatValidation(t *testing.T) {
{
Config: testAccCheckPagerDutyServiceConfig(username, email, escalationPolicy, ""),
PlanOnly: true,
ExpectError: regexp.MustCompile("Name can't be blank neither contain.*, nor non-printable characters."),
ExpectError: regexp.MustCompile(errMessageMatcher),
},
// Name with & in it
{
Config: testAccCheckPagerDutyServiceConfig(username, email, escalationPolicy, "this name has an ampersand (&)"),
PlanOnly: true,
ExpectError: regexp.MustCompile("Name can't be blank neither contain.*, nor non-printable characters."),
ExpectError: regexp.MustCompile(errMessageMatcher),
},
// Name with one white space at the end
{
Config: testAccCheckPagerDutyServiceConfig(username, email, escalationPolicy, "this name has a white space at the end "),
PlanOnly: true,
ExpectError: regexp.MustCompile(errMessageMatcher),
},
// Name with multiple white space at the end
{
Config: testAccCheckPagerDutyServiceConfig(username, email, escalationPolicy, "this name has white spaces at the end "),
PlanOnly: true,
ExpectError: regexp.MustCompile(errMessageMatcher),
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions pagerduty/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ func validateCantBeBlankOrNotPrintableChars(v interface{}, p cty.Path) diag.Diag
var diags diag.Diagnostics

value := v.(string)
matcher := regexp.MustCompile(`^$|^[ ]+$|[/\\<>&]`)
matcher := regexp.MustCompile(`^$|^[ ]+$|[/\\<>&]|\s+$`)
matches := matcher.MatchString(value)

if matches {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Name can't be blank neither contain '\\', '/', '&', '<', '>', nor non-printable characters.",
Summary: "Name must not be blank, nor contain the characters '\\', '/', '&', '<', '>', or any non-printable characters. White spaces at the end are also not allowed.",
AttributePath: p,
})
}
Expand Down

0 comments on commit 3a6043b

Please sign in to comment.