-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[Bug]: 使用v10.26.0版本导致了应用程序的崩溃 #1408
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
Comments
Hey @ccpwcn , sorry to hear about this issue. Nothing should have changed that would affected these validations, especially such core ones. Is it possible to provide a simpler reproducible example only using the validator code not through gin? Mainly so I can take a look tomorrow , it’s very late here, will help me debug faster 🙏 If not I can likely piece it together from your example. |
I tried really quickly to reproduce but was unable to using the below code. I even checked the git blame, the package main
import (
"fmt"
"github.com/go-playground/validator/v10"
)
type OrderItem struct {
SkuId int64 `json:"skuId,omitempty" form:"skuId" binding:"required,gte=1"`
Subject int8 `json:"subject,omitempty" form:"subject" binding:"required,oneof=1 2"`
Amount int64 `json:"amount,omitempty" form:"amount" binding:"required,gte=1"`
}
type OrderItemsCreateInput struct {
OrderItems []OrderItem `json:"orderItems,omitempty" form:"orderItems" binding:"required,dive,min=1,max=100"`
}
func main() {
validator := validator.New()
input := OrderItemsCreateInput{
OrderItems: []OrderItem{
{
SkuId: 3,
Subject: 2,
Amount: 3,
},
},
}
errs := validator.Struct(input)
fmt.Println(errs)
} |
@deankarn
|
Do you mean this exact same code fails on your computer @nodivbyzero ? What version of Go and OS are you using, I was using:
|
I used go version go1.23.2 windows/amd64, Windows 10, No more code for this bug, the code are customer private, but this bug is bound to occur. |
v10.14.0 no problem, v10.26.0 is bound to crash/panic. |
@deankarn not exactly the same. I changed My Go version:
Here is the unit-test which fails right now:
|
I am confused, the original bug report stack trace shows the gte validation erroring which is only on the inner struct. I see now though why it would fail, this should have never worked before, if it did it was a bug, because min and max shouldn’t work with a struct. |
But the current definition is not running min & max on a/the slice because they are after the dive , they are being applied to each OrderItem in the slice. If you want them to apply to the slice of OrderItem they need to come before the dive. This not me deflecting blame for having a bug, but this is also why it’s important to have unit tests to catch unexpected things before they can affect a customer. I highly recommend everyone always them :) |
I see, Are you saying that if I want to constrain the []OrderItems slice itself, I should place the constraint before the dive, and if I want to constrain every member of the []OrerItems slice, I should place it after the dive? What do I think is wrong with this? Because in a slice, there may not only be int types, but also other types. How can we use min and max to constrain them uniformly? |
What happened?
当我使用 v10.26.0 的时候,验证参数,导致了下面的崩溃:
当我退回 v10.14.0 的时候就没有问题了。
realMsgService/ctrl/order_item.go:173 是这样的:
这段代码我已经很久没有改过它,这一次崩溃,就是因为 github.com/go-playground/validator/v10 从 v10.14.0 升到了 v10.26.0 导致的。这给我造成了非常严重的客户信任危机。
代码中使用的结构体 param.OrderItemsCreateInput 是这样的:
Version
v10.26.0
Example Code
The text was updated successfully, but these errors were encountered: