From 0dd2271664a91eadcf287274b02ca5a9697b2b35 Mon Sep 17 00:00:00 2001 From: Ethan Dickson Date: Tue, 3 Sep 2024 08:23:10 +0000 Subject: [PATCH] chore: validate fields as per coderd --- internal/provider/group_resource.go | 7 ++++++- internal/provider/template_resource.go | 8 +++++++- internal/provider/user_resource.go | 7 +++++++ internal/provider/util.go | 7 +++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/internal/provider/group_resource.go b/internal/provider/group_resource.go index c7f11bd..eeb6ebe 100644 --- a/internal/provider/group_resource.go +++ b/internal/provider/group_resource.go @@ -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(""), }, diff --git a/internal/provider/template_resource.go b/internal/provider/template_resource.go index 9255f2f..2292491 100644 --- a/internal/provider/template_resource.go +++ b/internal/provider/template_resource.go @@ -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.", @@ -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{ diff --git a/internal/provider/user_resource.go b/internal/provider/user_resource.go index 4e8de49..6bec177 100644 --- a/internal/provider/user_resource.go +++ b/internal/provider/user_resource.go @@ -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.", diff --git a/internal/provider/util.go b/internal/provider/util.go index 03d899f..cdcee5b 100644 --- a/internal/provider/util.go +++ b/internal/provider/util.go @@ -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 } 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