38 lines
816 B
Go
38 lines
816 B
Go
package irisutils
|
|
|
|
import (
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/kataras/golog"
|
|
"github.com/kataras/iris/v12"
|
|
)
|
|
|
|
func ReadJSON(ctx iris.Context, reqData interface{}) bool {
|
|
if err := ctx.ReadJSON(reqData); err != nil {
|
|
golog.Errorf("Failed to parse input sent to %s by %s: %v", ctx.Path(), ctx.RemoteAddr(), err)
|
|
ctx.StopWithStatus(iris.StatusBadRequest)
|
|
return false
|
|
}
|
|
|
|
if vErr := Validator.Struct(reqData); vErr != nil {
|
|
if _, ok := vErr.(*validator.InvalidValidationError); ok {
|
|
return true
|
|
}
|
|
|
|
errOut := []ValidationError{}
|
|
for _, err := range vErr.(validator.ValidationErrors) {
|
|
errOut = append(errOut, ValidationError{
|
|
Field: err.Field(),
|
|
Tag: err.Tag(),
|
|
})
|
|
}
|
|
|
|
ctx.JSON(Resp{
|
|
Result: false,
|
|
Data: errOut,
|
|
})
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|