-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
I am planning to implement Auto Validation #4199
Comments
reference: func init() {
uni := ut.New(zh.New())
trans, _ := uni.GetTranslator("zh")
v := validator.New()
logx.Must(zh_translations.RegisterDefaultTranslations(v, trans))
httpx.SetValidator(&validate{validate: v})
}
type validate struct {
validate *validator.Validate
}
func (v *validate) Validate(r *http.Request, data any) error {
return v.validate.StructCtx(r.Context(), data)
} |
Is the code you provided a suggestion for implementation? I noticed that there are two interface: // [email protected]/rest/httpx/requests.go
// Validator defines the interface for validating the request.
type Validator interface {
// Validate validates the request and parsed data.
Validate(r *http.Request, data any) error
} // [email protected]/core/validation/validator.go
// Validator represents a validator.
type Validator interface {
// Validate validates the value.
Validate() error
} I’m curious about why there are two different interfaces. Could you please explain? |
Yes, just choose one of the two. go-zero/rest/httpx/requests.go Lines 55 to 59 in 01bbc78
|
go-zero/rest/httpx/requests.go Lines 55 to 59 in 01bbc78
go-zero/rest/httpx/requests.go Lines 31 to 35 in 01bbc78
question 1 question 2 |
|
For a struct, you can use a tag to register validation rules:
But what would be the way to register a validation rule for path or header? |
https://github.com/Linde7777/go-zero/tree/auto_validation/rest/httpx I have finished the part of validating struct and the lazy initialization |
for example: go-zero/rest/httpx/requests_test.go Lines 509 to 514 in 4a62d08
|
Yes, you can. |
go-zero/rest/httpx/requests_test.go Lines 509 to 514 in 4a62d08
It seems that the code you provide didn't answer "how to register validation rules for http.Request?" For register validation rule of http.Request, I have figured out two method: method 1: define the validation rule for http.Request in the struct
The tag "rv" is for "Request Validation". method 2: using a middlewareWe don't need to validate request in the interface |
go-zero provides
go-zero/core/validation/validator.go Lines 3 to 7 in 4a62d08
go-zero/rest/httpx/requests.go Line 28 in 4a62d08
go-zero/rest/httpx/requests.go Lines 126 to 128 in 4a62d08
I hope this answer answers your initial question. |
I got it, what about setting go-playground's validator as a default validator? I think many people would like to use it. The implementation will be lazy initialization. |
Hello, I don't think it's necessary, because go-zero already provides the ability to extend validation. But you can launch a PR to allow more people to see your code to further discuss the need for a merger. |
ok, thank you! |
https://medium.com/dev-genius/the-elegance-of-go-interface-b4c9c00012da I've written an post to introduce how to use |
Related issue(but stale): #3167
I am planning to implement such feature.
Is your feature request related to a problem? Please describe.
Current auto validation rules is not enough.
Describe the solution you'd like
Imitating Gin's validator implementation.
There is a tag "binding", once you add that tag to the struct, when you call
Bind()
-like function(in go-zero, it ishttpx.Parse()
), it will automatically validate the struct's fields.Gin underlying using the go-playground's validator, it has many validation rules, and also support custom validation rule.
Describe alternatives you've considered
Currently I have no alternatives for auto validation
Additional context
In Gin's implementation, the validation's interface has such function:
Validate(obj any) error
, and in go-zero, the validation interface's function isValidate() error
, I not sure which I should useThe text was updated successfully, but these errors were encountered: