hashicorp / hashicorp/terraform-plugin-framework-validators

RequiredWhenStringValueAtAnyOf()

Open
#113 2 comments 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
Go
Stars
28
Forks
13
Avg merge
2d 13h
Merged PRs (30d)
1

Description

### Terraform CLI and Framework Versions

any

### Use Cases or Problem Statement

I have a need for schema attributes to be recognized as mandatory when certain values appear in other attributes.

For example, when `mode` is `"complicated"`, `complicated_mode_params` should be required.

I'm thinking something like this:

```go
"mode": schema.StringAttribute{
Required: true,
Validators: []validator.String{stringvalidator.OneOf("simple", "complicated")},
},
"complicated_mode_params": schema.MapNestedAttribute{
Optional: true,
Validators: []validator.Map{
mapvalidator.RequiredWhenStringValueAtAnyOf("complicated",
path.Expressions{path.MatchRelative().AtName("mode")}...),
},
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"mode_one_param_one": schema.StringAttribute{
Required: true,
},
"mode_one_param_two": schema.StringAttribute{
Required: true,
},
},
},
},
```

### Proposal

This situation could be generalized with validators for each attribute type which check for trigger values in other attributes of all types:

```go
stringValidator.RequiredWhenStringValueAtAnyOf(val string, attributesToSumPathExpressions ...path.Expression) validator.String
stringValidator.RequiredWhenInt64ValueAtAnyOf(val int64, attributesToSumPathExpressions ...path.Expression) validator.String
stringValidator.RequiredWhenBoolValueAtAnyOf(val bool, attributesToSumPathExpressions ...path.Expression) validator.String
```

Same for `int64Validator`, `mapValidator`, `boolValidator`, etc...

Comments elsewhere from @bflad indicate you're planning to set ground rules for validator inclusion this Friday, so I'm opening this issue with that in mind.

If there's interest, I could take a crack at it.

### Additional Information

_No response_

### Code of Conduct

- [X] I agree to follow this project's Code of Conduct

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.