From 2a179d10657d58cc8ea63f7700f3a493a1c2e1a2 Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Wed, 14 Oct 2015 13:44:28 -0500 Subject: [PATCH] helper/schema: ValidateFunc support for maps --- helper/schema/schema.go | 15 +++++++++++++-- helper/schema/schema_test.go | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 34145b136..f4d860995 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -540,8 +540,8 @@ func (m schemaMap) InternalValidate(topSchemaMap schemaMap) error { if v.ValidateFunc != nil { switch v.Type { - case TypeList, TypeSet, TypeMap: - return fmt.Errorf("ValidateFunc is only supported on primitives.") + case TypeList, TypeSet: + return fmt.Errorf("ValidateFunc is not yet supported on lists or sets.") } } } @@ -1118,6 +1118,17 @@ func (m schemaMap) validateMap( } } + if schema.ValidateFunc != nil { + validatableMap := make(map[string]interface{}) + for _, raw := range raws { + for k, v := range raw.(map[string]interface{}) { + validatableMap[k] = v + } + } + + return schema.ValidateFunc(validatableMap, k) + } + return nil, nil } diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index faf703b0f..09eeef119 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -2903,7 +2903,7 @@ func TestSchemaMap_InternalValidate(t *testing.T) { { map[string]*Schema{ "foo": &Schema{ - Type: TypeMap, + Type: TypeSet, Required: true, ValidateFunc: func(v interface{}, k string) (ws []string, es []error) { return