Skip to content

fix: Improve friendly validation error messages #3390

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
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
15 changes: 8 additions & 7 deletions coderd/httpapi/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The semantics for username validation may be different to those of workspace and template names.
We should register different validators for each of these.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously these fields used the username validation tag, so behavior hasn't changed and I wasn't looking to change the behavior in this PR. If the validation is incorrect then we should definitely fix that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related #3321

})
if err != nil {
panic(err)
}
for _, tag := range []string{"username", "template_name", "workspace_name"} {
err := validate.RegisterValidation(tag, nameValidator)
if err != nil {
panic(err)
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion codersdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions codersdk/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
Expand Down