Skip to content

RequiredWhenStringValueAtAnyOf() #113

@chrismarget

Description

@chrismarget

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:

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

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

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions