helper/schema: Disallow validation+diff suppression on computed-only fields
This commit is contained in:
parent
eced7e39d4
commit
9867ce4dde
|
@ -645,6 +645,19 @@ func (m schemaMap) InternalValidate(topSchemaMap schemaMap) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Computed-only field
|
||||||
|
if v.Computed && !v.Optional {
|
||||||
|
if v.ValidateFunc != nil {
|
||||||
|
return fmt.Errorf("%s: ValidateFunc is for validating user input, "+
|
||||||
|
"there's nothing to validate on computed-only field", k)
|
||||||
|
}
|
||||||
|
if v.DiffSuppressFunc != nil {
|
||||||
|
return fmt.Errorf("%s: DiffSuppressFunc is for suppressing differences"+
|
||||||
|
" between config and state representation. "+
|
||||||
|
"There is no config for computed-only field, nothing to compare.", k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if v.ValidateFunc != nil {
|
if v.ValidateFunc != nil {
|
||||||
switch v.Type {
|
switch v.Type {
|
||||||
case TypeList, TypeSet:
|
case TypeList, TypeSet:
|
||||||
|
|
|
@ -3325,16 +3325,46 @@ func TestSchemaMap_InternalValidate(t *testing.T) {
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"computed-only field with validateFunc": {
|
||||||
|
map[string]*Schema{
|
||||||
|
"string": &Schema{
|
||||||
|
Type: TypeString,
|
||||||
|
Computed: true,
|
||||||
|
ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
|
||||||
|
es = append(es, fmt.Errorf("this is not fine"))
|
||||||
|
return
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
|
||||||
|
"computed-only field with diffSuppressFunc": {
|
||||||
|
map[string]*Schema{
|
||||||
|
"string": &Schema{
|
||||||
|
Type: TypeString,
|
||||||
|
Computed: true,
|
||||||
|
DiffSuppressFunc: func(k, old, new string, d *ResourceData) bool {
|
||||||
|
// Always suppress any diff
|
||||||
|
return false
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for tn, tc := range cases {
|
for tn, tc := range cases {
|
||||||
err := schemaMap(tc.In).InternalValidate(nil)
|
t.Run(tn, func(t *testing.T) {
|
||||||
if err != nil != tc.Err {
|
err := schemaMap(tc.In).InternalValidate(nil)
|
||||||
if tc.Err {
|
if err != nil != tc.Err {
|
||||||
t.Fatalf("%q: Expected error did not occur:\n\n%#v", tn, tc.In)
|
if tc.Err {
|
||||||
|
t.Fatalf("%q: Expected error did not occur:\n\n%#v", tn, tc.In)
|
||||||
|
}
|
||||||
|
t.Fatalf("%q: Unexpected error occurred: %s\n\n%#v", tn, err, tc.In)
|
||||||
}
|
}
|
||||||
t.Fatalf("%q: Unexpected error occurred:\n\n%#v", tn, tc.In)
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue