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:
Mathias Fredriksson
2022-08-09 14:25:23 +03:00
committed by GitHub
parent f62e1ede77
commit c0cc8b9935
3 changed files with 16 additions and 10 deletions

View File

@ -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)
}
}
}