Skip to content

feat: add tag and value in validation error details #1760

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

Merged
merged 5 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion coderd/httpapi/httpapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func Read(rw http.ResponseWriter, r *http.Request, value interface{}) bool {
for _, validationError := range validationErrors {
apiErrors = append(apiErrors, Error{
Field: validationError.Field(),
Detail: validationError.Tag(),
Detail: fmt.Sprintf("Validation failed for tag %q with value: \"%v\"", validationError.Tag(), validationError.Value()),
})
}
Write(rw, http.StatusBadRequest, Response{
Expand Down
8 changes: 4 additions & 4 deletions coderd/httpapi/httpapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestRead(t *testing.T) {

var validate toValidate
require.True(t, httpapi.Read(rw, r, &validate))
require.Equal(t, validate.Value, "hi")
require.Equal(t, "hi", validate.Value)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The expected and actual arguments were inverted so I changed them to what they should be.

})

t.Run("ValidateFailure", func(t *testing.T) {
Expand All @@ -75,8 +75,8 @@ func TestRead(t *testing.T) {
err := json.NewDecoder(rw.Body).Decode(&v)
require.NoError(t, err)
require.Len(t, v.Errors, 1)
require.Equal(t, v.Errors[0].Field, "value")
require.Equal(t, v.Errors[0].Detail, "required")
require.Equal(t, "value", v.Errors[0].Field)
require.Equal(t, "Validation failed for tag \"required\" with value: \"\"", v.Errors[0].Detail)
})
}

Expand Down Expand Up @@ -140,7 +140,7 @@ func TestReadUsername(t *testing.T) {
r := httptest.NewRequest("POST", "/", bytes.NewBuffer(data))

var validate toValidate
require.Equal(t, httpapi.Read(rw, r, &validate), testCase.Valid)
require.Equal(t, testCase.Valid, httpapi.Read(rw, r, &validate))
})
}
}
Expand Down