feat: add configurable validator for httpx.Parse (#2923)

Co-authored-by: qiying.wang <qiying.wang@highlight.mobi>
This commit is contained in:
Qiying Wang 2023-02-26 20:40:22 +08:00 committed by kevin
parent 956884a3ff
commit 366131640e

View File

@ -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.