mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: Improve friendly validation error messages (#3390)
* fix: Add validations to `(*codersdk.Error).Friendly` * fix: Add named validators for template and workspace name
This commit is contained in:
+6
-1
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user