Skip to content

fix: Improve friendly validation error messages - #3390

Merged
mafredri merged 2 commits into
mainfrom
mafredri/improve-cli-error-message-on-validation-failed
Aug 9, 2022
Merged

mafredri merged 2 commits into
mainfrom
mafredri/improve-cli-error-message-on-validation-failed

Conversation

@mafredri

@mafredri mafredri commented Aug 5, 2022 •

Copy link
Copy Markdown
Member

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:

❯ go run ./cmd/coder rename testy user/tester
Validation failed.
Run 'coder rename --help' for usage.

After:

❯ go run ./cmd/coder rename testy user/tester
Validation failed.
- name: Validation failed for tag "workspace_name" with value: "user/tester"
Run 'coder rename --help' for usage.

The original Validation failed for tag "username" was also confusing, so new validators were registered.

  • fix: Add validations to (*codersdk.Error).Friendly
  • fix: Add named validators for template and workspace name

Fixes #1673

@mafredri mafredri self-assigned this Aug 5, 2022
@mafredri
mafredri requested a review from a team August 5, 2022 12:03
Comment thread coderd/httpapi/httpapi.go
if !ok {
return false
}
return UsernameValid(str)

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Related #3321

@AbhineetJain

Copy link
Copy Markdown
Contributor

This PR seems related to #1943 and should be impacting the error messages that we show on frontend, so a verification of the frontend behavior and a review from @presleyp should be helpful.

@mafredri

mafredri commented Aug 8, 2022

Copy link
Copy Markdown
Member Author

@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 workspace_name instead of username.

@bpmct bpmct left a comment

Copy link
Copy Markdown
Member

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.

@mafredri

mafredri commented Aug 8, 2022

Copy link
Copy Markdown
Member Author

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.

@bpmct

bpmct commented Aug 8, 2022

Copy link
Copy Markdown
Member

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.

@Emyrk

Emyrk commented Aug 8, 2022

Copy link
Copy Markdown
Member

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.

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.

@mafredri

mafredri commented Aug 9, 2022

Copy link
Copy Markdown
Member Author

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.

@mafredri
mafredri merged commit c0cc8b9 into main Aug 9, 2022
@mafredri
mafredri deleted the mafredri/improve-cli-error-message-on-validation-failed branch August 9, 2022 11:25
@mafredri mafredri mentioned this pull request Aug 9, 2022
1 of 2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI: getting "Validation failed name: username" error in cli in case workspace or template name does not follow allowed pattern

5 participants