Merge pull request 'get body request, validator fix' (#3) from diana/core:develop into develop
Reviewed-on: #3
This commit is contained in:
commit
edc0c0c036
2 changed files with 34 additions and 8 deletions
30
context.go
30
context.go
|
@ -68,6 +68,36 @@ func (c *Context) RequestParamExists(key string) bool {
|
||||||
return c.Request.httpRequest.Form.Has(key)
|
return c.Request.httpRequest.Form.Has(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Context) GetRequesForm() interface{} {
|
||||||
|
return c.Request.httpRequest.Form
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Context) GetRequesBodyMap() map[string]interface{} {
|
||||||
|
var dat map[string]any
|
||||||
|
body := c.Request.httpRequest.Body
|
||||||
|
if body != nil {
|
||||||
|
if content, err := io.ReadAll(body); err == nil {
|
||||||
|
json.Unmarshal(content, &dat)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dat
|
||||||
|
}
|
||||||
|
|
||||||
|
// get json body and bind to dest interface
|
||||||
|
func (c *Context) GetRequesBodyStruct(dest interface{}) error {
|
||||||
|
body := c.Request.httpRequest.Body
|
||||||
|
if body != nil {
|
||||||
|
value := reflect.ValueOf(dest)
|
||||||
|
if value.Kind() != reflect.Ptr {
|
||||||
|
fmt.Println("dest is not a pointer")
|
||||||
|
return errors.New("dest is not a pointer")
|
||||||
|
}
|
||||||
|
err := json.NewDecoder(body).Decode(dest)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Context) GetHeader(key string) string {
|
func (c *Context) GetHeader(key string) string {
|
||||||
return c.Request.httpRequest.Header.Get(key)
|
return c.Request.httpRequest.Header.Get(key)
|
||||||
}
|
}
|
||||||
|
|
12
validator.go
12
validator.go
|
@ -33,18 +33,14 @@ func (v *Validator) Validate(data map[string]interface{}, rules map[string]inter
|
||||||
vr = validationResult{}
|
vr = validationResult{}
|
||||||
vr.hasFailed = false
|
vr.hasFailed = false
|
||||||
res := map[string]string{}
|
res := map[string]string{}
|
||||||
for key, val := range data {
|
for rule_key, rule_val := range rules {
|
||||||
_, ok := rules[key]
|
rls, err := parseRules(rule_val)
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
rls, err := parseRules(rules[key])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
err = validation.Validate(val, rls...)
|
err = validation.Validate(data[rule_key], rls...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res[key] = fmt.Sprintf("%v: %v", key, err.Error())
|
res[rule_key] = fmt.Sprintf("%v: %v", rule_key, err.Error())
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue