Use Go validator framework to succinctly validate requests#74
Merged
Conversation
Collaborator
Author
|
@bgentry This is a pretty good option for getting some better request validation going on. Thoughts? |
b63b776 to
d6afc82
Compare
bgentry
approved these changes
Jun 29, 2024
Contributor
bgentry
left a comment
There was a problem hiding this comment.
Looks solid, excited to try this out!
Comment on lines
+42
to
+45
| switch kind { | ||
| case reflect.Float32: | ||
| fallthrough | ||
| case reflect.Float64: | ||
| fallthrough | ||
| case reflect.Int: | ||
| fallthrough | ||
| case reflect.Int32: | ||
| fallthrough | ||
| case reflect.Int64: | ||
| message += fmt.Sprintf(" Field `%s` must be less than or equal to %s.", | ||
| fieldErr.Field(), fieldErr.Param()) |
Contributor
There was a problem hiding this comment.
do you prefer this style with fallthrough over putting all the values in a single case clause?
Collaborator
Author
There was a problem hiding this comment.
I find it nominally easier to read because it's less cramped, but no strong opinion either way. The one line with commas is probably more conventional, so I changed it to that.
A general problem with Go is that validating things is a very noisy
affair involving long lists of if statements combined with error returns
for every field on a struct.
A technique that we've been using for a while is to use the Go validator
framework [1] that allows fields to be tagged with succinct validation
syntax for a variety of different things. e.g.
type jobCancelRequest struct {
JobIDs []int64String `json:"ids" validate:"required,min=1,max=1000"`
}
I'm not sure the use of something like this is necessary for a project
that's a dependency like core River itself, but but internal use on more
of an "application" project like River UI, it might be helpful.
We combine the validator with the new API framework from #63 so that
incoming request structs are validated automatically for every endpoint,
which shaves a lot of lines of otherwise necessary validation code out
of individual API endpoint definitions.
[1] https://github.com/go-playground/validator?tab=readme-ov-file
d6afc82 to
9c22a9b
Compare
Collaborator
Author
|
ty. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A general problem with Go is that validating things is a very noisy
affair involving long lists of if statements combined with error returns
for every field on a struct.
A technique that we've been using for a while is to use the Go validator
framework [1] that allows fields to be tagged with succinct validation
syntax for a variety of different things. e.g.
I'm not sure the use of something like this is necessary for a project
that's a dependency like core River itself, but but internal use on more
of an "application" project like River UI, it might be helpful.
We combine the validator with the new API framework from #63 so that
incoming request structs are validated automatically for every endpoint,
which shaves a lot of lines of otherwise necessary validation code out
of individual API endpoint definitions.
[1] https://github.com/go-playground/validator?tab=readme-ov-file