diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 8766e10b0..558871ddb 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -132,7 +132,10 @@ type Schema struct { // what do to about the removed attribute. Removed string - // ValidateFunc allows individual fields to validate + // ValidateFunc allows individual fields to define arbitrary validation + // logic. It is yielded the provided config value as an interface{} that is + // guaranteed to be of the proper Schema type, and it can yield warnings or + // errors based on inspection of that value. ValidateFunc SchemaValidateFunc } @@ -1183,7 +1186,7 @@ func (m schemaMap) validateType( "%q: [REMOVED] %s", k, schema.Removed)) } - if schema.ValidateFunc != nil { + if len(es) == 0 && schema.ValidateFunc != nil { ws2, es2 := schema.ValidateFunc(raw) for _, w := range ws2 { ws = append(ws, fmt.Sprintf("%q: %s", k, w)) diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index e23f51279..36aa2e137 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -3438,6 +3438,23 @@ func TestSchemaMap_Validate(t *testing.T) { fmt.Errorf(`"validate_me": something is not right here`), }, }, + + "ValidateFunc not called when type does not match": { + Schema: map[string]*Schema{ + "number": &Schema{ + Type: TypeInt, + Required: true, + ValidateFunc: func(v interface{}) (ws []string, es []error) { + t.Fatalf("Should not have gotten validate call") + return + }, + }, + }, + Config: map[string]interface{}{ + "number": "NaN", + }, + Err: true, + }, } for tn, tc := range cases {