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

30
iris.go
View File

@@ -155,6 +155,9 @@ type Application struct {
// See `Context#Tr` method for request-based translations.
I18n *i18n.I18n
// Validator is the request body validator, defaults to nil.
Validator context.Validator
// view engine
view view.View
// used for build
@@ -309,6 +312,33 @@ func (app *Application) I18nReadOnly() context.I18nReadOnly {
return app.I18n
}
// Validate validates a value and returns nil if passed or
// the failure reason if does not.
func (app *Application) Validate(v interface{}) error {
if app.Validator == nil {
return nil
}
// val := reflect.ValueOf(v)
// if val.Kind() == reflect.Ptr && !val.IsNil() {
// val = val.Elem()
// }
// if val.Kind() == reflect.Struct && val.Type() != timeType {
// return app.Validator.Struct(v)
// }
// no need to check the kind, underline lib does it but in the future this may change (look above).
err := app.Validator.Struct(v)
if err != nil {
if !strings.HasPrefix(err.Error(), "validator: ") {
return err
}
}
return nil
}
var (
// HTML view engine.
// Shortcut of the kataras/iris/view.HTML.