Skip to content

Commit 59e396f

Browse files
committed
fix: app display name validation
1 parent aef6220 commit 59e396f

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

provider/app.go

+13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ var (
2323
appSlugRegex = regexp.MustCompile(`^[a-z0-9](-?[a-z0-9])*$`)
2424
)
2525

26+
const appDisplayNameMaxLength = 64 // database column limit
27+
2628
func appResource() *schema.Resource {
2729
return &schema.Resource{
2830
SchemaVersion: 1,
@@ -124,6 +126,17 @@ func appResource() *schema.Resource {
124126
Description: "A display name to identify the app. Defaults to the slug.",
125127
ForceNew: true,
126128
Optional: true,
129+
ValidateDiagFunc: func(val interface{}, c cty.Path) diag.Diagnostics {
130+
valStr, ok := val.(string)
131+
if !ok {
132+
return diag.Errorf("expected string, got %T", val)
133+
}
134+
135+
if len(valStr) > appDisplayNameMaxLength {
136+
return diag.Errorf("display name is too long (max %d characters)", appDisplayNameMaxLength)
137+
}
138+
return nil
139+
},
127140
},
128141
"subdomain": {
129142
Type: schema.TypeBool,

provider/app_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,62 @@ func TestApp(t *testing.T) {
415415
}
416416
})
417417

418+
t.Run("DisplayName", func(t *testing.T) {
419+
t.Parallel()
420+
421+
cases := []struct {
422+
name string
423+
displayName string
424+
expectValue string
425+
expectError *regexp.Regexp
426+
}{
427+
{
428+
name: "Empty",
429+
displayName: "",
430+
},
431+
{
432+
name: "Regular",
433+
displayName: "Regular Application",
434+
},
435+
{
436+
name: "DisplayNameTooLong",
437+
displayName: "01234567890123456789012345678901234567890123456789012345678901234",
438+
expectError: regexp.MustCompile("display name is too long"),
439+
},
440+
}
441+
442+
for _, c := range cases {
443+
c := c
444+
445+
t.Run(c.name, func(t *testing.T) {
446+
t.Parallel()
447+
448+
config := fmt.Sprintf(`
449+
provider "coder" {
450+
}
451+
resource "coder_agent" "dev" {
452+
os = "linux"
453+
arch = "amd64"
454+
}
455+
resource "coder_app" "code-server" {
456+
agent_id = coder_agent.dev.id
457+
slug = "code-server"
458+
display_name = "%s"
459+
url = "http://localhost:13337"
460+
open_in = "slim-window"
461+
}
462+
`, c.displayName)
463+
464+
resource.Test(t, resource.TestCase{
465+
ProviderFactories: coderFactory(),
466+
IsUnitTest: true,
467+
Steps: []resource.TestStep{{
468+
Config: config,
469+
ExpectError: c.expectError,
470+
}},
471+
})
472+
})
473+
}
474+
})
475+
418476
}

0 commit comments

Comments
 (0)