Skip to content

chore: validate fields as per coderd #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion internal/provider/group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,18 @@ func (r *GroupResource) Schema(ctx context.Context, req resource.SchemaRequest,
"name": schema.StringAttribute{
MarkdownDescription: "The unique name of the group.",
Required: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 36),
stringvalidator.RegexMatches(nameValidRegex, "Group names must be alpahnumeric with hyphens."),
},
},
"display_name": schema.StringAttribute{
MarkdownDescription: "The display name of the group. Defaults to the group name.",
Computed: true,
Optional: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
stringvalidator.LengthBetween(1, 64),
stringvalidator.RegexMatches(displayNameRegex, "Group display names must be alphanumeric with spaces"),
},
Default: stringdefault.StaticString(""),
},
Expand Down
8 changes: 7 additions & 1 deletion internal/provider/template_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,17 @@ func (r *TemplateResource) Schema(ctx context.Context, req resource.SchemaReques
Required: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 32),
stringvalidator.RegexMatches(nameValidRegex, "Template names must be alphanumeric with hyphens."),
},
},
"display_name": schema.StringAttribute{
MarkdownDescription: "The display name of the template. Defaults to the template name.",
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 64),
stringvalidator.RegexMatches(displayNameRegex, "Template display names must be alphanumeric with spaces."),
},
},
"description": schema.StringAttribute{
MarkdownDescription: "A description of the template.",
Expand Down Expand Up @@ -394,7 +399,8 @@ func (r *TemplateResource) Schema(ctx context.Context, req resource.SchemaReques
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
stringvalidator.LengthBetween(1, 64),
stringvalidator.RegexMatches(templateVersionNameRegex, "Template version names must be alphanumeric with underscores and dots."),
},
},
"message": schema.StringAttribute{
Expand Down
7 changes: 7 additions & 0 deletions internal/provider/user_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,18 @@ func (r *UserResource) Schema(ctx context.Context, req resource.SchemaRequest, r
"username": schema.StringAttribute{
MarkdownDescription: "Username of the user.",
Required: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 32),
stringvalidator.RegexMatches(nameValidRegex, "Username must be alphanumeric with hyphens."),
},
},
"name": schema.StringAttribute{
MarkdownDescription: "Display name of the user. Defaults to username.",
Computed: true,
Optional: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 128),
},
},
"email": schema.StringAttribute{
MarkdownDescription: "Email address of the user.",
Expand Down
7 changes: 7 additions & 0 deletions internal/provider/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"

"github.com/google/uuid"
)

var (
nameValidRegex = regexp.MustCompile("^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$")
templateVersionNameRegex = regexp.MustCompile(`^[a-zA-Z0-9]+(?:[_.-]{1}[a-zA-Z0-9]+)*$`)
displayNameRegex = regexp.MustCompile(`^[^\s](.*[^\s])?$`)
)

func PtrTo[T any](v T) *T {
return &v
}
Expand Down
Loading