From 920a2a63a7283743bb69bd78ef9019ee2867387a Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Fri, 5 Aug 2022 14:43:37 +0300 Subject: [PATCH 1/2] fix: Add validations to `(*codersdk.Error).Friendly` --- codersdk/client.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/codersdk/client.go b/codersdk/client.go index 1279d477053a4..e7668a34c2843 100644 --- a/codersdk/client.go +++ b/codersdk/client.go @@ -186,7 +186,12 @@ func (e *Error) StatusCode() int { } func (e *Error) Friendly() string { - return fmt.Sprintf("%s. %s", strings.TrimSuffix(e.Message, "."), e.Helper) + var sb strings.Builder + _, _ = fmt.Fprintf(&sb, "%s. %s", strings.TrimSuffix(e.Message, "."), e.Helper) + for _, err := range e.Validations { + _, _ = fmt.Fprintf(&sb, "\n- %s: %s", err.Field, err.Detail) + } + return sb.String() } func (e *Error) Error() string { From ae6508cca012f5b2697cc740867a40ff97304708 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Fri, 5 Aug 2022 14:44:12 +0300 Subject: [PATCH 2/2] fix: Add named validators for template and workspace name --- coderd/httpapi/httpapi.go | 15 ++++++++------- codersdk/organizations.go | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/coderd/httpapi/httpapi.go b/coderd/httpapi/httpapi.go index 03a876c7b6374..b42d2257b45b5 100644 --- a/coderd/httpapi/httpapi.go +++ b/coderd/httpapi/httpapi.go @@ -14,9 +14,7 @@ import ( "github.com/coder/coder/codersdk" ) -var ( - validate *validator.Validate -) +var validate *validator.Validate // This init is used to create a validator and register validation-specific // functionality for the HTTP API. @@ -31,16 +29,19 @@ func init() { } return name }) - err := validate.RegisterValidation("username", func(fl validator.FieldLevel) bool { + nameValidator := func(fl validator.FieldLevel) bool { f := fl.Field().Interface() str, ok := f.(string) if !ok { return false } return UsernameValid(str) - }) - if err != nil { - panic(err) + } + for _, tag := range []string{"username", "template_name", "workspace_name"} { + err := validate.RegisterValidation(tag, nameValidator) + if err != nil { + panic(err) + } } } diff --git a/codersdk/organizations.go b/codersdk/organizations.go index 2d3c3b3a9224a..400c010f9d15b 100644 --- a/codersdk/organizations.go +++ b/codersdk/organizations.go @@ -48,7 +48,7 @@ type CreateTemplateVersionRequest struct { // CreateTemplateRequest provides options when creating a template. type CreateTemplateRequest struct { // Name is the name of the template. - Name string `json:"name" validate:"username,required"` + Name string `json:"name" validate:"template_name,required"` // Description is a description of what the template contains. It must be // less than 128 bytes. Description string `json:"description,omitempty" validate:"lt=128"` @@ -75,7 +75,7 @@ type CreateTemplateRequest struct { // CreateWorkspaceRequest provides options for creating a new workspace. type CreateWorkspaceRequest struct { TemplateID uuid.UUID `json:"template_id" validate:"required"` - Name string `json:"name" validate:"username,required"` + Name string `json:"name" validate:"workspace_name,required"` AutostartSchedule *string `json:"autostart_schedule"` TTLMillis *int64 `json:"ttl_ms,omitempty"` // ParameterValues allows for additional parameters to be provided