Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the attribute from the request to at-least-one validator's error message #199

Merged

Conversation

ChihweiLHBird
Copy link
Contributor

@ChihweiLHBird ChihweiLHBird commented Mar 5, 2024

In the schema:

var resourceSchema = schema.Schema{
	Attributes: map[string]schema.Attribute{
		// ... many other attributes
		"attribute1": schema.Int64Attribute{
			Validators: []validator.Int64{
				int64validator.AtLeastOneOf(path.MatchRoot("attribute2")),
			},
			Optional:    true,
			Computed:    true,
		},
	},
}

Let's don't put both attributes in the Terraform config file.

Previous error message:

│ Error: Invalid Attribute Combination
│ 
│   with my_reource.my-resource-instance,
│   on main.tf line 22, in resource "my_reource" "my-resource-instance":
│   22: resource "my_reource" "my-resource-instance" {
│ 
│ At least one attribute out of [attribute2] must be specified

This message above is misleading because it actually requires one of attribute1 and attribute2, but attribute1 is not in the error message.

Now with the change in this PR:

│ Error: Invalid Attribute Combination
│ 
│   with my_reource.my-resource-instance,
│   on main.tf line 22, in resource "my_reource" "my-resource-instance":
│   22: resource "my_reource" "my-resource-instance" {
│ 
│ At least one attribute out of [attribute1,attribute2] must be specified

We can correctly inform the user what attributes are required.

@ChihweiLHBird ChihweiLHBird requested a review from a team as a code owner March 5, 2024 21:51
@ChihweiLHBird ChihweiLHBird changed the title Add the attribute to requiring at least one attributes validator's error message Add the attribute from the request to at-least-one validator's error message Mar 6, 2024
@bendbennett
Copy link
Contributor

Hi @ChihweiLHBird 👋

Thank you for this submission.

Could I ask that you add test coverage to verify the behaviour of this change?
Perhaps a new test to confirm that both the attribute upon which the AtLeastOneOf validator is declared, and all of the attributes supplied in the path.Expression args appear in the diagnostic.

Could you also add a change log entry as described in the New Pull Request section of the doc on contributing?

Thanks again.

@bendbennett bendbennett added the bug Something isn't working label Mar 6, 2024
@bendbennett bendbennett added this to the v0.12.1 milestone Mar 6, 2024
@ChihweiLHBird
Copy link
Contributor Author

Hi @bendbennett, thanks for letting me know! I just added them.

Copy link
Contributor

@bendbennett bendbennett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding the change log entry, and updating the tests.

How would you feel about the suggested changes to the tests, in which expErrors int, and expDiagMsgSegs []string are dropped in favour of using expected *schemavalidator.AtLeastOneOfValidatorResponse?

With the suggested changes, the following would also need to be updated:

testCases := map[string]testCase{
		"base": {
			/* ... */
			expected: &schemavalidator.AtLeastOneOfValidatorResponse{},
		},
		"self-is-null": {
			/* ... */
			expected: &schemavalidator.AtLeastOneOfValidatorResponse{},
		},
		"error_none-set": {
			/* ... */
			expected: &schemavalidator.AtLeastOneOfValidatorResponse{
				Diagnostics: diag.Diagnostics{
					diag.WithPath(
						path.Root("bar"),
						diag.NewErrorDiagnostic(
							"Invalid Attribute Combination",
							"At least one attribute out of [foo,baz,bar] must be specified",
						),
					),
				},
			},
		},
		"multiple-set": {
			/* ... */
			expected: &schemavalidator.AtLeastOneOfValidatorResponse{},
		},
		"allow-duplicate-input": {
			/* ... */
			expected: &schemavalidator.AtLeastOneOfValidatorResponse{},
		},
		"unknowns": {
			/* ... */
			expected: &schemavalidator.AtLeastOneOfValidatorResponse{},
		},
		"matches-no-attribute-in-schema": {
			/* ... */
			expected: &schemavalidator.AtLeastOneOfValidatorResponse{
				Diagnostics: diag.Diagnostics{
					diag.NewErrorDiagnostic(
						"Invalid Path Expression for Schema",
						"The Terraform Provider unexpectedly provided a path expression that does not match the current schema. "+
							"This can happen if the path expression does not correctly follow the schema in structure or types. "+
							"Please report this to the provider developers.\n\nPath Expression: fooz",
					),
					diag.WithPath(
						path.Root("bar"),
						diag.NewErrorDiagnostic(
							"Invalid Attribute Combination",
							"At least one attribute out of [fooz,bar] must be specified",
						),
					),
				},
			},
		},
	}

	for name, test := range testCases {
		name, test := name, test
		t.Run(name, func(t *testing.T) {
			t.Parallel()
			res := &schemavalidator.AtLeastOneOfValidatorResponse{}

			schemavalidator.AtLeastOneOfValidator{
				PathExpressions: test.in,
			}.Validate(context.TODO(), test.req, res)

			if diff := cmp.Diff(test.expected, res); diff != "" {
				t.Errorf("unexpected diff: %s", diff)
			}
		})
	}

Interested to hear your thoughts.

internal/schemavalidator/at_least_one_of_test.go Outdated Show resolved Hide resolved
internal/schemavalidator/at_least_one_of_test.go Outdated Show resolved Hide resolved
internal/schemavalidator/at_least_one_of_test.go Outdated Show resolved Hide resolved
internal/schemavalidator/at_least_one_of_test.go Outdated Show resolved Hide resolved
internal/schemavalidator/at_least_one_of_test.go Outdated Show resolved Hide resolved
Co-authored-by: Benjamin Bennett <[email protected]>
@ChihweiLHBird
Copy link
Contributor Author

ChihweiLHBird commented Mar 10, 2024

Hi @bflad, thanks for the suggestion! I think this suggestion is fine, but one downside is that in the future, if any error message is changed in the validator, the tests will need to be adjusted accordingly. This is somehow against the design principle of SSOT and might lead to more effort of maintenance required in the future IMO. However, they are only few error messages, thus the impact is pretty minimum and I think it would be fine. If you think it's okay, I can proceed to this way.

Copy link
Contributor

@bendbennett bendbennett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ChihweiLHBird,

Thank you for updating the PR.

The point you make about interrogating the contents of the diagnostics is a good one, but I think in this instance it is warranted as we want to be sure of the contents and structure of the diagnostic error message. This pattern has been used elsewhere in this repository, such as in TestConflictingValidatorValidate.

internal/schemavalidator/at_least_one_of_test.go Outdated Show resolved Hide resolved
Co-authored-by: Benjamin Bennett <[email protected]>
@bendbennett
Copy link
Contributor

@ChihweiLHBird thank you very much for your PR. This looks good to me.

@bendbennett bendbennett merged commit 8663d64 into hashicorp:main Mar 12, 2024
6 checks passed
@ChihweiLHBird ChihweiLHBird deleted the zhiwei/fix-at-least-one-err-msg branch March 12, 2024 12:55
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 14, 2024
@bflad bflad modified the milestones: v0.12.1, v0.13.0 Apr 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants