helper/schema: skip ValidateFunc on other errors

Guarantees that the `interface{}` arg to ValidateFunc is the proper
type, allowing implementations to be simpler.

Finish the docstring on `ValidateFunc` to call this out.

/cc @mitchellh
This commit is contained in:
Paul Hinze 2015-06-08 08:51:04 -05:00
parent 37b234e42b
commit 49352db26f
2 changed files with 22 additions and 2 deletions

View File

@ -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))

View File

@ -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 {