-
Notifications
You must be signed in to change notification settings - Fork 90
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
feat: [#416] Support add custom filter for validation #516
Conversation
Codecov ReportAll modified and coverable lines are covered by tests β
Additional details and impacted files@@ Coverage Diff @@
## master #516 +/- ##
==========================================
+ Coverage 69.90% 69.98% +0.07%
==========================================
Files 180 180
Lines 11000 11025 +25
==========================================
+ Hits 7690 7716 +26
+ Misses 2739 2738 -1
Partials 571 571 β View full report in Codecov by Sentry. |
contracts/validation/validation.go
Outdated
@@ -5,10 +5,16 @@ type Option func(map[string]any) | |||
type Validation interface { | |||
// Make create a new validator instance. | |||
Make(data any, rules map[string]string, options ...Option) (Validator, error) | |||
// AddFilter add the custom filter. | |||
AddFilter(name string, filterFunc any) Validation |
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.
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.
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.
Maybe:
type Filter interface {
// Signature set the unique signature of the filter.
Signature() string
// Passes determine if the validation rule passes.
Handle() func(val any) (any, error)
}
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.
Nah, it's not a good idea since the filter function can vary.
We can only support one style: func(val any) (any, error)
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.
A developer can also create a filter function like this:
func Default(val string, def ...string) string {
...
}
which can be used like this:
map[string]string{
"name" : "default:krishan"
}
// assume registered as "default"
So, let's not add constraints on developers since gookit/validate provides this feature.
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.
Maybe:
type Filter interface { // Signature set the unique signature of the filter. Signature() string // Passes determine if the validation rule passes. Handle() func(val any) (any, error) }
we can change it to ?
type Filter interface {
// Signature set the unique signature of the filter.
Signature() string
// Passes determine if the validation rule passes.
Handle() any // will mention in docs that any can only be two type of function
}
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.
We also need to add a new Filter
method in the FormRequest
:

Users can use the Filter feature through the Filter
method, instead of set a tag in the struct:

The way of set a tag in the struct is inconsistent with the rule, so I suggest optimize it. cc @devhaozi
So we need to fdmovd the logic in goravel/gin and goravel/fiber and add some new logic:

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.
LGTM
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.
Great PR π Please add some integration tests in goravel/example
// Signature sets the unique signature of the filter. | ||
Signature() string | ||
|
||
// Handle defines the filter function to apply. |
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.
Great
Co-authored-by: Wenbo Han <[email protected]>
Co-authored-by: Wenbo Han <[email protected]>
π Description
Closes goravel/goravel#416
β Checks