mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-03 00:38:40 +08:00
feat: add configurable validator for httpx.Parse (#2923)
Co-authored-by: qiying.wang <qiying.wang@highlight.mobi>
This commit is contained in:
parent
956884a3ff
commit
366131640e
@ -23,8 +23,17 @@ const (
|
||||
var (
|
||||
formUnmarshaler = mapping.NewUnmarshaler(formKey, mapping.WithStringValues())
|
||||
pathUnmarshaler = mapping.NewUnmarshaler(pathKey, mapping.WithStringValues())
|
||||
xValidator Validator
|
||||
)
|
||||
|
||||
type Validator interface {
|
||||
Validate(data interface{}, lang string) error
|
||||
}
|
||||
|
||||
func SetValidator(validator Validator) {
|
||||
xValidator = validator
|
||||
}
|
||||
|
||||
// Parse parses the request.
|
||||
func Parse(r *http.Request, v interface{}) error {
|
||||
if err := ParsePath(r, v); err != nil {
|
||||
@ -39,7 +48,14 @@ func Parse(r *http.Request, v interface{}) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return ParseJsonBody(r, v)
|
||||
if err := ParseJsonBody(r, v); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if xValidator != nil {
|
||||
return xValidator.Validate(v, r.Header.Get("Accept-Language"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ParseHeaders parses the headers request.
|
||||
|
Loading…
Reference in New Issue
Block a user