mirror of
https://github.com/rancher/norman.git
synced 2025-09-03 00:06:24 +00:00
Validate subtypes in create/update
This commit is contained in:
@@ -23,11 +23,5 @@ func ParseAndValidateBody(apiContext *types.APIContext, create bool) (map[string
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if apiContext.Schema.Validator != nil {
|
|
||||||
if err := apiContext.Schema.Validator(apiContext, data); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
@@ -20,6 +20,7 @@ var (
|
|||||||
type Operation string
|
type Operation string
|
||||||
|
|
||||||
type Builder struct {
|
type Builder struct {
|
||||||
|
apiContext *types.APIContext
|
||||||
Version *types.APIVersion
|
Version *types.APIVersion
|
||||||
Schemas *types.Schemas
|
Schemas *types.Schemas
|
||||||
RefValidator types.ReferenceValidator
|
RefValidator types.ReferenceValidator
|
||||||
@@ -27,6 +28,7 @@ type Builder struct {
|
|||||||
|
|
||||||
func NewBuilder(apiRequest *types.APIContext) *Builder {
|
func NewBuilder(apiRequest *types.APIContext) *Builder {
|
||||||
return &Builder{
|
return &Builder{
|
||||||
|
apiContext: apiRequest,
|
||||||
Version: apiRequest.Version,
|
Version: apiRequest.Version,
|
||||||
Schemas: apiRequest.Schemas,
|
Schemas: apiRequest.Schemas,
|
||||||
RefValidator: apiRequest.ReferenceValidator,
|
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) {
|
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 {
|
func (b *Builder) copyInputs(schema *types.Schema, input map[string]interface{}, op Operation, result map[string]interface{}) error {
|
||||||
|
@@ -50,7 +50,7 @@ type RequestHandler func(request *APIContext) error
|
|||||||
|
|
||||||
type QueryFilter func(opts *QueryOptions, data []map[string]interface{}) []map[string]interface{}
|
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)
|
type Formatter func(request *APIContext, resource *RawResource)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user