mirror of
https://github.com/opentffoundation/opentf.git
synced 2026-03-14 04:01:09 -04:00
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:
@@ -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))
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user