I have a resource which has computed + optional true but when adding a ValidateDiagFunc
it throws ValidateDiagFunc is for validating user input, there's nothing to validate on computed-only field
if the field is not set.]
Is it possible to add validation to optional+computed schemas?
Simple example below the fails resource will throw InternalValidate
error.
"data_store_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateDiagFunc: func(value interface{}, path cty.Path) diag.Diagnostics {
v := value.(string)
r, _ := regexp.Compile(`^[a-zA-Z0-9._-]+$`)
if !r.MatchString(v) {
return diag.Errorf("the data_store_id can only contain alphanumeric characters, dash, dot and underscore.")
}
return nil
},
},
resource "example" "works" {
data_store_id = "works"
}
resource "example" "fails" {
}