Validated func with computed + optional

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


Hmm. My first instinct is this should be possible, and I could’ve sworn other resources use this functionality. At least, I’d be very surprised if this particular combination isn’t in use in any other resources. Any chance you’d be willing to open an issue and capture the details there?

Hi @paddy ,

I think i just had something misconfigured somewhere - came back to this and it does work as expected.

1 Like