feat: [#416] Support add custom filter for validation#516
feat: [#416] Support add custom filter for validation#516krishankumar01 merged 8 commits intomasterfrom
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
| // 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.
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.
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.
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.
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.
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:
hwbrzzl
left a comment
There was a problem hiding this comment.
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. |
Co-authored-by: Wenbo Han <hwbrzzl@gmail.com>
Co-authored-by: Wenbo Han <hwbrzzl@gmail.com>


📑 Description
Closes goravel/goravel#416
✅ Checks