1
0
mirror of https://github.com/rancher/norman.git synced 2025-09-02 07:44:51 +00:00

Validate subtypes in create/update

This commit is contained in:
Darren Shepherd
2018-01-17 16:33:05 -07:00
parent 9e6ea56442
commit 942bde06d7
3 changed files with 13 additions and 8 deletions

View File

@@ -23,11 +23,5 @@ func ParseAndValidateBody(apiContext *types.APIContext, create bool) (map[string
return nil, err
}
if apiContext.Schema.Validator != nil {
if err := apiContext.Schema.Validator(apiContext, data); err != nil {
return nil, err
}
}
return data, nil
}

View File

@@ -20,6 +20,7 @@ var (
type Operation string
type Builder struct {
apiContext *types.APIContext
Version *types.APIVersion
Schemas *types.Schemas
RefValidator types.ReferenceValidator
@@ -27,6 +28,7 @@ type Builder struct {
func NewBuilder(apiRequest *types.APIContext) *Builder {
return &Builder{
apiContext: apiRequest,
Version: apiRequest.Version,
Schemas: apiRequest.Schemas,
RefValidator: apiRequest.ReferenceValidator,
@@ -34,7 +36,16 @@ func NewBuilder(apiRequest *types.APIContext) *Builder {
}
func (b *Builder) Construct(schema *types.Schema, input map[string]interface{}, op Operation) (map[string]interface{}, error) {
return b.copyFields(schema, input, op)
result, err := b.copyFields(schema, input, op)
if err != nil {
return nil, err
}
if (op == Create || op == Update) && schema.Validator != nil {
if err := schema.Validator(b.apiContext, schema, result); err != nil {
return nil, err
}
}
return result, nil
}
func (b *Builder) copyInputs(schema *types.Schema, input map[string]interface{}, op Operation, result map[string]interface{}) error {

View File

@@ -50,7 +50,7 @@ type RequestHandler func(request *APIContext) error
type QueryFilter func(opts *QueryOptions, data []map[string]interface{}) []map[string]interface{}
type Validator func(request *APIContext, data map[string]interface{}) error
type Validator func(request *APIContext, schema *Schema, data map[string]interface{}) error
type Formatter func(request *APIContext, resource *RawResource)