1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 02:17:05 +00:00

add 'app.Validator' field for ReadJSON, ReadXML, ReadMsgPack, ReadYAML, ReadForm, ReadQuery data validation, defaults to empty but can be set-ed to 3rd-party packages

Former-commit-id: e42d9be5928edcdaad4579c008f741b1a7d97da9
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-04-09 19:02:08 +03:00
parent 1b02f048ef
commit ad154ea479
6 changed files with 166 additions and 112 deletions

View File

@@ -2602,6 +2602,15 @@ func GetBody(r *http.Request, resetBody bool) ([]byte, error) {
return data, nil
}
// Validator is the validator for request body on Context methods such as
// ReadJSON, ReadMsgPack, ReadXML, ReadYAML, ReadForm, ReadQuery, ReadBody and e.t.c.
type Validator interface {
Struct(interface{}) error
// If community asks for more than a struct validation on JSON, XML, MsgPack, Form, Query and e.t.c
// then we should add more methods here, alternative approach would be to have a
// `Validator:Validate(interface{}) error` and a map[reflect.Kind]Validator instead.
}
// UnmarshalBody reads the request's body and binds it to a value or pointer of any type
// Examples of usage: context.ReadJSON, context.ReadXML.
//
@@ -2636,7 +2645,12 @@ func (ctx *context) UnmarshalBody(outPtr interface{}, unmarshaler Unmarshaler) e
// we don't need to reduce the performance here by using the reflect.TypeOf method.
// f the v doesn't contains a self-body decoder use the custom unmarshaler to bind the body.
return unmarshaler.Unmarshal(rawData, outPtr)
err = unmarshaler.Unmarshal(rawData, outPtr)
if err != nil {
return err
}
return ctx.Application().Validate(outPtr)
}
func (ctx *context) shouldOptimize() bool {
@@ -2687,7 +2701,12 @@ func (ctx *context) ReadForm(formObject interface{}) error {
return nil
}
return schema.DecodeForm(values, formObject)
err := schema.DecodeForm(values, formObject)
if err != nil {
return err
}
return ctx.Application().Validate(formObject)
}
// ReadQuery binds url query to "ptr". The struct field tag is "url".
@@ -2699,7 +2718,12 @@ func (ctx *context) ReadQuery(ptr interface{}) error {
return nil
}
return schema.DecodeQuery(values, ptr)
err := schema.DecodeQuery(values, ptr)
if err != nil {
return err
}
return ctx.Application().Validate(ptr)
}
// ReadProtobuf binds the body to the "ptr" of a proto Message and returns any error.
@@ -2719,7 +2743,12 @@ func (ctx *context) ReadMsgPack(ptr interface{}) error {
return err
}
return msgpack.Unmarshal(rawData, ptr)
err = msgpack.Unmarshal(rawData, ptr)
if err != nil {
return err
}
return ctx.Application().Validate(ptr)
}
// ReadBody binds the request body to the "ptr" depending on the HTTP Method and the Request's Content-Type.