-
Notifications
You must be signed in to change notification settings - Fork 881
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
fix: Improve friendly validation error messages #3390
Conversation
@@ -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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related #3321
@AbhineetJain thanks for keeping an eye out. I don't think this affects the frontend though, except for the validation error message when creating a workspace, if the workspace name is invalid. Then the error message will say that the validator that failed is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we able to surface the regex and/or what characters violated it? From looking at the code, it is clear you cannot use spaces and/or underscores but this may not be immediately clear to the user.
AFAICT, not very straight-forward with the validator library we use. I looked a bit through the API and validators are mostly booleans, so we'd need to utilize multiple validators for one field to be able to narrow down the error case? Perhaps we should look into replacing the library, or just implement custom types with their own JSON marshal/unmarshal where we can control the behavior. |
Oh, that makes sense. Perhaps something we can look into later if it comes up again. |
I noticed this too with the validator lib before. There is something you can do with their "translator" api,but it seems kinda jank. I wish the validator custom functions could return custom errors. |
Agreed, using the translations API seems very jank. I found one project utilizing it, but that's a lot of code for working around the fact that the validator API has shortcomings: https://github.com/Aptomi/k8s-app-engine/blob/11b495fc520680093a990fee0eaaf598507596b5/pkg/lang/validation.go There are a few other libs that seem to have a better API:
But I have no experience with these. |
This PR adds the validation errors to the friendly error message.
I found it confusing when working on #3000 and all I got was
Validation failed.
.Before:
After:
The original
Validation failed for tag "username"
was also confusing, so new validators were registered.(*codersdk.Error).Friendly
Fixes #1673