diff --git a/api/handler/validate.go b/api/handler/validate.go index 353fc5f5..50830bb6 100644 --- a/api/handler/validate.go +++ b/api/handler/validate.go @@ -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 } diff --git a/parse/builder/builder.go b/parse/builder/builder.go index ffeaf2a9..bb76d0d5 100644 --- a/parse/builder/builder.go +++ b/parse/builder/builder.go @@ -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 { diff --git a/types/server_types.go b/types/server_types.go index b10fcbed..0c609f25 100644 --- a/types/server_types.go +++ b/types/server_types.go @@ -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)