Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 21bd3ee

Browse files
chore: validate fields as per coderd (#85)
1 parent 7645de8 commit 21bd3ee

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

internal/provider/group_resource.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,18 @@ func (r *GroupResource) Schema(ctx context.Context, req resource.SchemaRequest,
7777
"name": schema.StringAttribute{
7878
MarkdownDescription: "The unique name of the group.",
7979
Required: true,
80+
Validators: []validator.String{
81+
stringvalidator.LengthBetween(1, 36),
82+
stringvalidator.RegexMatches(nameValidRegex, "Group names must be alpahnumeric with hyphens."),
83+
},
8084
},
8185
"display_name": schema.StringAttribute{
8286
MarkdownDescription: "The display name of the group. Defaults to the group name.",
8387
Computed: true,
8488
Optional: true,
8589
Validators: []validator.String{
86-
stringvalidator.LengthAtLeast(1),
90+
stringvalidator.LengthBetween(1, 64),
91+
stringvalidator.RegexMatches(displayNameRegex, "Group display names must be alphanumeric with spaces"),
8792
},
8893
Default: stringdefault.StaticString(""),
8994
},

internal/provider/template_resource.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,17 @@ func (r *TemplateResource) Schema(ctx context.Context, req resource.SchemaReques
248248
Required: true,
249249
Validators: []validator.String{
250250
stringvalidator.LengthBetween(1, 32),
251+
stringvalidator.RegexMatches(nameValidRegex, "Template names must be alphanumeric with hyphens."),
251252
},
252253
},
253254
"display_name": schema.StringAttribute{
254255
MarkdownDescription: "The display name of the template. Defaults to the template name.",
255256
Optional: true,
256257
Computed: true,
258+
Validators: []validator.String{
259+
stringvalidator.LengthBetween(1, 64),
260+
stringvalidator.RegexMatches(displayNameRegex, "Template display names must be alphanumeric with spaces."),
261+
},
257262
},
258263
"description": schema.StringAttribute{
259264
MarkdownDescription: "A description of the template.",
@@ -394,7 +399,8 @@ func (r *TemplateResource) Schema(ctx context.Context, req resource.SchemaReques
394399
Optional: true,
395400
Computed: true,
396401
Validators: []validator.String{
397-
stringvalidator.LengthAtLeast(1),
402+
stringvalidator.LengthBetween(1, 64),
403+
stringvalidator.RegexMatches(templateVersionNameRegex, "Template version names must be alphanumeric with underscores and dots."),
398404
},
399405
},
400406
"message": schema.StringAttribute{

internal/provider/user_resource.go

+7
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,18 @@ func (r *UserResource) Schema(ctx context.Context, req resource.SchemaRequest, r
7171
"username": schema.StringAttribute{
7272
MarkdownDescription: "Username of the user.",
7373
Required: true,
74+
Validators: []validator.String{
75+
stringvalidator.LengthBetween(1, 32),
76+
stringvalidator.RegexMatches(nameValidRegex, "Username must be alphanumeric with hyphens."),
77+
},
7478
},
7579
"name": schema.StringAttribute{
7680
MarkdownDescription: "Display name of the user. Defaults to username.",
7781
Computed: true,
7882
Optional: true,
83+
Validators: []validator.String{
84+
stringvalidator.LengthBetween(1, 128),
85+
},
7986
},
8087
"email": schema.StringAttribute{
8188
MarkdownDescription: "Email address of the user.",

internal/provider/util.go

+7
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9+
"regexp"
910

1011
"github.com/google/uuid"
1112
)
1213

14+
var (
15+
nameValidRegex = regexp.MustCompile("^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$")
16+
templateVersionNameRegex = regexp.MustCompile(`^[a-zA-Z0-9]+(?:[_.-]{1}[a-zA-Z0-9]+)*$`)
17+
displayNameRegex = regexp.MustCompile(`^[^\s](.*[^\s])?$`)
18+
)
19+
1320
func PtrTo[T any](v T) *T {
1421
return &v
1522
}

0 commit comments

Comments
 (0)