Framework Attribute Validation: a differentFrom() validator?

I’ve got a scenario where I need to ensure that the user hasn’t configured the same value for two different attributes:

resource "foo" "foo" {
  attr1 = 50
  attr2 = 60
  attr3 = 50
  attrDifferentFrom1or2or3 = <anything other than 50 or 60 is okay here>

These attributes aren’t actually right at the top level of a resource/datasource, but rather in a deeply nested object, so I think I’m looking for an attribute validator.

None of the built-in validators jump out at me as a solution, and right now it’s a bunch of clunky business logic invoked by the resource’s ValidateConfig() method.

A proper validator seems cleaner, so I’m planning to write a uniqueAmong() or differentFrom() validator so I can do:

		"attrDifferentFrom1or2or3": schema.Int64Attribute{
			Validators: []validator.Int64{

Am I on the right track here?

Does this look like it might be useful to others? If so, I can take a crack at the full complement of string/int64/set/list/object validators and submit a PR…

Hi @hQnVyLRx ,

I think that you are on the right track with implementing a custom attribute validator. I suspect an implementation for your use case would probably look similar to the conflictsWith() attribute validators implementation except that you would compare the configValue with the mpValue in line 91.

I think a differentFrom() validator might be useful to others so feel free to open a PR if you have a full implementation or submit an issue in terraform-plugin-framework-validators if you want to make a more formal feature request and we’ll take a look at it.

1 Like

PR #110 submitted.

1 Like