-
Notifications
You must be signed in to change notification settings - Fork 11
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
refactor: separate validation, response and business logic #1301
base: staging
Are you sure you want to change the base?
Changes from all commits
a64d55c
3cb6800
f1f285e
6f40d13
500906e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package api_test | ||
|
||
import ( | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,8 +1,8 @@ | ||||||
package handlers | ||||||
|
||||||
import ( | ||||||
"bytes" | ||||||
"cmp" | ||||||
"encoding/base64" | ||||||
"encoding/json" | ||||||
"fmt" | ||||||
"io" | ||||||
|
@@ -95,6 +95,13 @@ func (v *validationError) hasErrors() bool { | |||||
return v != nil && len(*v) > 0 | ||||||
} | ||||||
|
||||||
func (v *validationError) AsError() error { | ||||||
if v.hasErrors() { | ||||||
return newBadRequestErr("%s", v.Error()) | ||||||
} | ||||||
return nil | ||||||
} | ||||||
|
||||||
// -------------------------------------- | ||||||
|
||||||
func (v *validationError) checkRegex(regex *regexp.Regexp, param, paramName string) string { | ||||||
|
@@ -142,14 +149,8 @@ func (v *validationError) checkUserEmailToken(token string) string { | |||||
|
||||||
// check request structure (body contains valid json and all required parameters are present) | ||||||
// return error only if internal error occurs, otherwise add error to validationError and/or return nil | ||||||
func (v *validationError) checkBody(data interface{}, r *http.Request) error { | ||||||
// check if content type is application/json | ||||||
if contentType := r.Header.Get("Content-Type"); !reJsonContentType.MatchString(contentType) { | ||||||
v.add("request body", "'Content-Type' header must be 'application/json'") | ||||||
} | ||||||
Comment on lines
-147
to
-149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't this check be kept somehow? Maybe move it to middleware, like if there is a body present it should be |
||||||
|
||||||
bodyBytes, err := io.ReadAll(r.Body) | ||||||
r.Body = io.NopCloser(bytes.NewReader(bodyBytes)) // unconsume body for error logging | ||||||
func (v *validationError) checkBody(data interface{}, requestBody io.ReadCloser) error { // <- should be checkBody after refactoring | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
bodyBytes, err := io.ReadAll(requestBody) | ||||||
if err != nil { | ||||||
return newInternalServerErr("error reading request body") | ||||||
} | ||||||
|
@@ -560,3 +561,28 @@ func (v *validationError) checkTimestamps(r *http.Request, chartLimits ChartTime | |||||
return afterTs, beforeTs | ||||||
} | ||||||
} | ||||||
|
||||||
func (v *validationError) checkDashboardId(id string) interface{} { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks essentially like the same as https://github.com/gobitfly/beaconchain/blob/staging/backend/pkg/api/handlers/handler_service.go#L85-L109 to me, what's the reason for this? Would be cool if it was possible to merge them |
||||||
if reInteger.MatchString(id) { | ||||||
// given id is a normal id | ||||||
id := v.checkUint(id, "dashboard_id") | ||||||
return types.VDBIdPrimary(id) | ||||||
} | ||||||
if reValidatorDashboardPublicId.MatchString(id) { | ||||||
// given id is a public id | ||||||
return types.VDBIdPublic(id) | ||||||
} | ||||||
// given id must be an encoded set of validators | ||||||
decodedId, err := base64.RawURLEncoding.DecodeString(id) | ||||||
if err != nil { | ||||||
v.add("dashboard_id", fmt.Sprintf("given value '%s' is not a valid dashboard id", id)) | ||||||
return nil | ||||||
} | ||||||
var validatorListError validationError | ||||||
indexes, publicKeys := validatorListError.checkValidatorList(string(decodedId), forbidEmpty) | ||||||
if validatorListError.hasErrors() { | ||||||
v.add("dashboard_id", fmt.Sprintf("given value '%s' is not a valid dashboard id", id)) | ||||||
return nil | ||||||
} | ||||||
return validatorSet{Indexes: indexes, PublicKeys: publicKeys} | ||||||
} |
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.
Imo it's worth re-thinking naming for the interface and method name here,
Check(er)
is a bit vague. It's only supposed to be used in the context of input validation, right? MaybeInputValidator
andValidate()
or so