Skip to content

Commit d0f5e21

Browse files
authored
fix: use codersdk functions for validating name attributes (#130)
1 parent 4b8a4b1 commit d0f5e21

File tree

11 files changed

+114
-24
lines changed

11 files changed

+114
-24
lines changed

.golangci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Visit https://golangci-lint.run/ for usage documentation
2-
# and information on other useful linters
1+
# Visit https://golangci-lint.run/ for usage documentation and information on
2+
# other useful linters
33
issues:
44
max-per-linter: 0
55
max-same-issues: 0
@@ -24,4 +24,4 @@ linters:
2424
- unconvert
2525
- unparam
2626
- unused
27-
- vet
27+
- vet
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package codersdkvalidator
2+
3+
import (
4+
"github.com/coder/coder/v2/codersdk"
5+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
6+
)
7+
8+
func DisplayName() validator.String {
9+
return validatorFromFunc(codersdk.DisplayNameValid, "value must be a valid display name")
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package codersdkvalidator
2+
3+
import (
4+
"github.com/coder/coder/v2/codersdk"
5+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
6+
)
7+
8+
func GroupName() validator.String {
9+
return validatorFromFunc(codersdk.GroupNameValid, "value must be a valid group name")
10+
}

internal/codersdkvalidator/name.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package codersdkvalidator
2+
3+
import (
4+
"github.com/coder/coder/v2/codersdk"
5+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
6+
)
7+
8+
func Name() validator.String {
9+
return validatorFromFunc(codersdk.NameValid, "value must be a valid name")
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package codersdkvalidator
2+
3+
import (
4+
"github.com/coder/coder/v2/codersdk"
5+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
6+
)
7+
8+
func TemplateVersionName() validator.String {
9+
return validatorFromFunc(codersdk.TemplateVersionNameValid, "value must be a valid template version name")
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package codersdkvalidator
2+
3+
import (
4+
"github.com/coder/coder/v2/codersdk"
5+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
6+
)
7+
8+
func UserRealName() validator.String {
9+
return validatorFromFunc(codersdk.UserRealNameValid, "value must be a valid name for a user")
10+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package codersdkvalidator
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
7+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
8+
)
9+
10+
type functionValidator struct {
11+
check func(string) error
12+
defaultMessage string
13+
err error
14+
}
15+
16+
func validatorFromFunc(check func(string) error, defaultMessage string) functionValidator {
17+
return functionValidator{
18+
check: check,
19+
defaultMessage: defaultMessage,
20+
}
21+
}
22+
23+
var _ validator.String = functionValidator{}
24+
25+
func (v functionValidator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
26+
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
27+
return
28+
}
29+
30+
name := req.ConfigValue.ValueString()
31+
if v.err = v.check(name); v.err != nil {
32+
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
33+
req.Path,
34+
v.Description(ctx),
35+
name,
36+
))
37+
}
38+
}
39+
40+
var _ validator.Describer = functionValidator{}
41+
42+
func (v functionValidator) Description(_ context.Context) string {
43+
if v.err != nil {
44+
return v.err.Error()
45+
}
46+
return v.defaultMessage
47+
}
48+
49+
func (v functionValidator) MarkdownDescription(ctx context.Context) string {
50+
return v.Description(ctx)
51+
}

internal/provider/group_resource.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"strings"
77

88
"github.com/coder/coder/v2/codersdk"
9+
"github.com/coder/terraform-provider-coderd/internal/codersdkvalidator"
910
"github.com/google/uuid"
10-
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1111
"github.com/hashicorp/terraform-plugin-framework/attr"
1212
"github.com/hashicorp/terraform-plugin-framework/diag"
1313
"github.com/hashicorp/terraform-plugin-framework/path"
@@ -77,17 +77,15 @@ func (r *GroupResource) Schema(ctx context.Context, req resource.SchemaRequest,
7777
MarkdownDescription: "The unique name of the group.",
7878
Required: true,
7979
Validators: []validator.String{
80-
stringvalidator.LengthBetween(1, 36),
81-
stringvalidator.RegexMatches(nameValidRegex, "Group names must be alpahnumeric with hyphens."),
80+
codersdkvalidator.GroupName(),
8281
},
8382
},
8483
"display_name": schema.StringAttribute{
8584
MarkdownDescription: "The display name of the group. Defaults to the group name.",
8685
Computed: true,
8786
Optional: true,
8887
Validators: []validator.String{
89-
stringvalidator.LengthBetween(1, 64),
90-
stringvalidator.RegexMatches(displayNameRegex, "Group display names must be alphanumeric with spaces"),
88+
codersdkvalidator.DisplayName(),
9189
},
9290
Default: stringdefault.StaticString(""),
9391
},

internal/provider/template_resource.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/coder/coder/v2/coderd/util/ptr"
1414
"github.com/coder/coder/v2/codersdk"
1515
"github.com/coder/coder/v2/provisionersdk"
16+
"github.com/coder/terraform-provider-coderd/internal/codersdkvalidator"
1617
"github.com/google/uuid"
1718
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
1819
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
@@ -258,17 +259,15 @@ func (r *TemplateResource) Schema(ctx context.Context, req resource.SchemaReques
258259
MarkdownDescription: "The name of the template.",
259260
Required: true,
260261
Validators: []validator.String{
261-
stringvalidator.LengthBetween(1, 32),
262-
stringvalidator.RegexMatches(nameValidRegex, "Template names must be alphanumeric with hyphens."),
262+
codersdkvalidator.Name(),
263263
},
264264
},
265265
"display_name": schema.StringAttribute{
266266
MarkdownDescription: "The display name of the template. Defaults to the template name.",
267267
Optional: true,
268268
Computed: true,
269269
Validators: []validator.String{
270-
stringvalidator.LengthBetween(1, 64),
271-
stringvalidator.RegexMatches(displayNameRegex, "Template display names must be alphanumeric with spaces."),
270+
codersdkvalidator.DisplayName(),
272271
},
273272
},
274273
"description": schema.StringAttribute{
@@ -418,8 +417,7 @@ func (r *TemplateResource) Schema(ctx context.Context, req resource.SchemaReques
418417
Optional: true,
419418
Computed: true,
420419
Validators: []validator.String{
421-
stringvalidator.LengthBetween(1, 64),
422-
stringvalidator.RegexMatches(templateVersionNameRegex, "Template version names must be alphanumeric with underscores and dots."),
420+
codersdkvalidator.TemplateVersionName(),
423421
},
424422
},
425423
"message": schema.StringAttribute{

internal/provider/user_resource.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/hashicorp/terraform-plugin-log/tflog"
2323

2424
"github.com/coder/coder/v2/codersdk"
25+
"github.com/coder/terraform-provider-coderd/internal/codersdkvalidator"
2526
)
2627

2728
// Ensure provider defined types fully satisfy framework interfaces.
@@ -71,16 +72,15 @@ func (r *UserResource) Schema(ctx context.Context, req resource.SchemaRequest, r
7172
MarkdownDescription: "Username of the user.",
7273
Required: true,
7374
Validators: []validator.String{
74-
stringvalidator.LengthBetween(1, 32),
75-
stringvalidator.RegexMatches(nameValidRegex, "Username must be alphanumeric with hyphens."),
75+
codersdkvalidator.Name(),
7676
},
7777
},
7878
"name": schema.StringAttribute{
7979
MarkdownDescription: "Display name of the user. Defaults to username.",
8080
Computed: true,
8181
Optional: true,
8282
Validators: []validator.String{
83-
stringvalidator.LengthBetween(1, 128),
83+
codersdkvalidator.UserRealName(),
8484
},
8585
},
8686
"email": schema.StringAttribute{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy