Repository navigation
fix: Improve friendly validation error messages - #3390
Conversation
| if !ok { | ||
| return false | ||
| } | ||
| return UsernameValid(str) |
There was a problem hiding this comment.
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.
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.
|
@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 |
bpmct
left a comment
There was a problem hiding this comment.
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).FriendlyFixes #1673