-
Notifications
You must be signed in to change notification settings - Fork 85
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
fix: Validator Bind can't cast value #336
Conversation
Codecov ReportAttention:
... and 2 files with indirect coverage changes π’ Thoughts on this report? Let us know! |
issue link to goravel/fiber/pull/36 |
I found the |
After more testing, https://www.goravel.dev/the-basics/validation.html#int is not resolved because the json.Unmarshal used internally by gookit/validate does not handle this case. |
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.
This test file has some error rules, I fixed it.
var dataType reflect.Kind | ||
switch data := data.(type) { | ||
var dataFace validate.DataFace | ||
var err error | ||
switch td := data.(type) { | ||
case validate.DataFace: | ||
dataFace = td | ||
case map[string]any: | ||
if len(data) == 0 { | ||
if len(td) == 0 { | ||
return nil, errors.New("data can't be empty") | ||
} | ||
dataType = reflect.Map | ||
} | ||
|
||
val := reflect.ValueOf(data) | ||
indirectVal := reflect.Indirect(val) | ||
typ := indirectVal.Type() | ||
if indirectVal.Kind() == reflect.Struct && typ != reflect.TypeOf(time.Time{}) { | ||
dataType = reflect.Struct | ||
} | ||
|
||
var dataFace validate.DataFace | ||
switch dataType { | ||
case reflect.Map: | ||
dataFace = validate.FromMap(data.(map[string]any)) | ||
case reflect.Struct: | ||
var err error | ||
dataFace = validate.FromMap(td) | ||
case url.Values: | ||
dataFace = validate.FromURLValues(td) | ||
case map[string][]string: | ||
dataFace = validate.FromURLValues(td) | ||
default: |
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.
I refactored it by referring to the New
method of github.com/gookit/validate
expectData: Data{A: "c"}, | ||
expectData: Data{A: ""}, |
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.
Now validator only Bind safe data, it meens validation must passed, so I change some test data.
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.
You don't need to release this fix to v1.13.x, right? because it's a breaking change.
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.
You don't need to release this fix to v1.13.x, right? because it's a breaking change.
Yes
This now can be solve use See: goravel/fiber#38 |
Co-authored-by: Wenbo Han <[email protected]>
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.
Perfect!
Closes goravel/goravel#302
π Description
β Checks
βΉ Additional Information