Skip to content

Use Go validator framework to succinctly validate requests#74

Merged
brandur merged 1 commit intomasterfrom
brandur-validate
Jun 29, 2024
Merged

Use Go validator framework to succinctly validate requests#74
brandur merged 1 commit intomasterfrom
brandur-validate

Conversation

@brandur
Copy link
Collaborator

@brandur brandur commented Jun 28, 2024

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

@brandur brandur requested a review from bgentry June 28, 2024 04:20
@brandur
Copy link
Collaborator Author

brandur commented Jun 28, 2024

@bgentry This is a pretty good option for getting some better request validation going on. Thoughts?

@brandur brandur force-pushed the brandur-validate branch from b63b776 to d6afc82 Compare June 28, 2024 04:27
Copy link
Contributor

@bgentry bgentry left a comment

Choose a reason for hiding this comment

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

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())
Copy link
Contributor

Choose a reason for hiding this comment

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

do you prefer this style with fallthrough over putting all the values in a single case clause?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

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
@brandur brandur force-pushed the brandur-validate branch from d6afc82 to 9c22a9b Compare June 29, 2024 15:21
@brandur
Copy link
Collaborator Author

brandur commented Jun 29, 2024

ty.

@brandur brandur merged commit 99bde67 into master Jun 29, 2024
@brandur brandur deleted the brandur-validate branch June 29, 2024 15:27
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.

2 participants