ValidateFunc deprecation in terraform-plugin-sdk v2

Hello, we are creating a new terraform provider. After updating SDK to v2.0.0-rc2 we got a deprecation warning in ValidateFunc (ValidateDiagFunc should be used), but methods in validation package (github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation) return schema.SchemaValidateFunc. Did we miss something?

An example

"media": {
  Type: schema.TypeString,
  Required: true,
  ValidateFunc: validation.StringInSlice([]string{"cdrom", "disk"}, false),
}

Thanks for pointing us into the right direction :).

1 Like

Nope, you got everything, this is just a weird situation we were working around with the release. We’ll get ValidateDiagFunc versions of those helpers created, in the meantime you can probably do a wrapper like

func validateDiagFunc(ctx context.Context, validateFunc func(interface{}, string) ([]string, []error)) schema.SchemaValidateDiagFunc {
  return func(i interface{}, path cty.Path) diag.Diagnostics {
    warnings, errs := validateFunc(i, fmt.Sprintf("%+v", path)
    var diags diag.Diagnostics
    for _, warning := range warnings {
      diags = append(diags, diag.Diagnostic{
        Severity: diag.Warning,
        Summary: warning,
      })
    }
    for _, err := range errs {
      diags = append(diags, diag.Diagnostic{
        Severity: diag.Error,
        Summary: err.Error(),
      })
    }
  }
}

Then you can use it like

"media": {
  Type: schema.TypeString,
  Required: true,
  ValidateDiagFunc: validateDiagFunc(validation.StringInSlice([]string{"cdrom", "disk"}, false)),
}

Obviously that’s pretty gnarly (and was written in the text box of a forums post without being tested, so take it with a grain of salt) but hopefully it gets the idea across. We’ll work on getting a better option into the SDK properly. I see you opened an issue for it here, which I appreciate, and we’ll definitely update that as we get this supported.

Many thanks for your answer :hugs:!
Since the issue will be tracked in GitHub, we can close the issue here.