From 366131640ee44214ca1b50bb4e6fc793bca68bdf Mon Sep 17 00:00:00 2001 From: Qiying Wang <781345688@qq.com> Date: Sun, 26 Feb 2023 20:40:22 +0800 Subject: [PATCH] feat: add configurable validator for httpx.Parse (#2923) Co-authored-by: qiying.wang --- rest/httpx/requests.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/rest/httpx/requests.go b/rest/httpx/requests.go index e46025b5..14a53327 100644 --- a/rest/httpx/requests.go +++ b/rest/httpx/requests.go @@ -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.