1
0
mirror of https://github.com/rancher/norman.git synced 2025-08-31 06:35:09 +00:00
Files
norman/api/handler/validate.go

34 lines
681 B
Go
Raw Normal View History

2017-11-21 13:46:30 -07:00
package handler
import (
"github.com/rancher/norman/parse"
"github.com/rancher/norman/parse/builder"
"github.com/rancher/norman/types"
)
2017-12-11 20:58:43 -07:00
func ParseAndValidateBody(apiContext *types.APIContext, create bool) (map[string]interface{}, error) {
2017-11-21 13:46:30 -07:00
data, err := parse.Body(apiContext.Request)
if err != nil {
return nil, err
}
b := builder.NewBuilder(apiContext)
2017-12-11 20:58:43 -07:00
op := builder.Create
if !create {
op = builder.Update
}
data, err = b.Construct(apiContext.Schema, data, op)
2017-11-28 14:28:25 -07:00
if err != nil {
return nil, err
}
if apiContext.Schema.Validator != nil {
if err := apiContext.Schema.Validator(apiContext, data); err != nil {
2017-11-21 13:46:30 -07:00
return nil, err
}
}
return data, nil
}