Skip to content

Commit c0cc8b9

Browse files
authored
fix: Improve friendly validation error messages (#3390)
* fix: Add validations to `(*codersdk.Error).Friendly` * fix: Add named validators for template and workspace name
1 parent f62e1ed commit c0cc8b9

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

coderd/httpapi/httpapi.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ import (
1414
"github.com/coder/coder/codersdk"
1515
)
1616

17-
var (
18-
validate *validator.Validate
19-
)
17+
var validate *validator.Validate
2018

2119
// This init is used to create a validator and register validation-specific
2220
// functionality for the HTTP API.
@@ -31,16 +29,19 @@ func init() {
3129
}
3230
return name
3331
})
34-
err := validate.RegisterValidation("username", func(fl validator.FieldLevel) bool {
32+
nameValidator := func(fl validator.FieldLevel) bool {
3533
f := fl.Field().Interface()
3634
str, ok := f.(string)
3735
if !ok {
3836
return false
3937
}
4038
return UsernameValid(str)
41-
})
42-
if err != nil {
43-
panic(err)
39+
}
40+
for _, tag := range []string{"username", "template_name", "workspace_name"} {
41+
err := validate.RegisterValidation(tag, nameValidator)
42+
if err != nil {
43+
panic(err)
44+
}
4445
}
4546
}
4647

codersdk/client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,12 @@ func (e *Error) StatusCode() int {
186186
}
187187

188188
func (e *Error) Friendly() string {
189-
return fmt.Sprintf("%s. %s", strings.TrimSuffix(e.Message, "."), e.Helper)
189+
var sb strings.Builder
190+
_, _ = fmt.Fprintf(&sb, "%s. %s", strings.TrimSuffix(e.Message, "."), e.Helper)
191+
for _, err := range e.Validations {
192+
_, _ = fmt.Fprintf(&sb, "\n- %s: %s", err.Field, err.Detail)
193+
}
194+
return sb.String()
190195
}
191196

192197
func (e *Error) Error() string {

codersdk/organizations.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type CreateTemplateVersionRequest struct {
4848
// CreateTemplateRequest provides options when creating a template.
4949
type CreateTemplateRequest struct {
5050
// Name is the name of the template.
51-
Name string `json:"name" validate:"username,required"`
51+
Name string `json:"name" validate:"template_name,required"`
5252
// Description is a description of what the template contains. It must be
5353
// less than 128 bytes.
5454
Description string `json:"description,omitempty" validate:"lt=128"`
@@ -75,7 +75,7 @@ type CreateTemplateRequest struct {
7575
// CreateWorkspaceRequest provides options for creating a new workspace.
7676
type CreateWorkspaceRequest struct {
7777
TemplateID uuid.UUID `json:"template_id" validate:"required"`
78-
Name string `json:"name" validate:"username,required"`
78+
Name string `json:"name" validate:"workspace_name,required"`
7979
AutostartSchedule *string `json:"autostart_schedule"`
8080
TTLMillis *int64 `json:"ttl_ms,omitempty"`
8181
// ParameterValues allows for additional parameters to be provided

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