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

Adding Set element validation for ValuesAre #36

Merged
merged 8 commits into from
Jun 23, 2022
Merged

Conversation

bendbennett
Copy link
Contributor

Closes: #12

@bendbennett bendbennett requested a review from a team as a code owner June 21, 2022 09:38
Copy link
Contributor

@detro detro left a comment

Choose a reason for hiding this comment

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

Left a bunch of comments, but overall this looks ready.

Once attr.Path is done, a lot of stuff will need rebasing and merging :)

Comment on lines 59 to 61
for _, validator := range v.valueValidators {
validator.Validate(ctx, request, resp)
if resp.Diagnostics.HasError() {
return
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: what do you think of the idea of running all .valueValidators against elem before leaving the function?

I don't have a string argument pro/against, but I'm thinking (similarly to ComposeAggregateTestCheckFunc in our testing), getting a report of all the reasons my input is wrong upfront, can be useful.

Of course, it can also be seen as "overwhelming", so I'm on the fence but I thought I'd share my thinking.

Copy link
Member

Choose a reason for hiding this comment

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

For what its worth (and I think its a good idea, but can be part of a later iteration), helper/validation.All(), which was widely used aggregated errors:

https://github.com/hashicorp/terraform-plugin-sdk/blob/ab572994a48576754ec619691e23bd9afdd7042d/helper/validation/meta.go#L32-L43

I'm having a hard time imagining a case where a "sequential" validation must be performed, as if some later validator was panicking, then it should be fixed to not panic. Giving all diagnostics allows practitioners to fix their Terraform configurations knowing all the rules at once, similar to what @detro is mentioning above in testing, rather than needing to repeatedly validate to find the next issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, have amended to run all validators before exiting.

descriptions = append(descriptions, validator.Description(ctx))
}

return fmt.Sprintf("value must satisfy all validations: %s", strings.Join(descriptions, " + "))
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return fmt.Sprintf("value must satisfy all validations: %s", strings.Join(descriptions, " + "))
return fmt.Sprintf("Attribute value must satisfy the following: %s", strings.Join(descriptions, " + "))

Just a suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Think I'm gonna leave this as is if that's ok.


type testCase struct {
val attr.Value
valuesAreValidators []tfsdk.AttributeValidator
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use the actual attribute validator here, instead of hosting only it's input?

Just curious.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thought I'd follow the same pattern we're using in other validator tests (e.g., int64validator.at_least_test.go).

Comment on lines 40 to 46
func AttributeValueTerraformValueDiagnostic(path *tftypes.AttributePath, description string, value string) diag.Diagnostic {
return diag.NewAttributeErrorDiagnostic(
path,
"Invalid Attribute Value Terraform Value",
capitalize(description)+", err: "+value,
)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

As discussed in our 1-2-1, I think this conversion will be made unnecessary by @bflad work on attr.Path.

Plus I think it will be really confusing for users to see an error that says "we were unable to convert your value in a terraform value". How would they know what to make of it?

Copy link
Member

Choose a reason for hiding this comment

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

Indeed this should go away with the new attribute path work. My recommendation in the meantime would be prefer messaging that says to contact the provider developers, similar to a lot of what is in the framework today (and hopefully going away more and more as part of hashicorp/terraform-plugin-framework#172).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have updated the message.

@bendbennett
Copy link
Contributor Author

Following discussion with @detro we could consolidate the ValuesAre validators for List, Map and Set into a single generic validator if we don't require any type-specific validation.

@bflad bflad added this to the v0.3.0 milestone Jun 22, 2022
Copy link
Member

@bflad bflad left a comment

Choose a reason for hiding this comment

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

Looks good to me 🚀

gotSetElems, gotOk := validateSet(context.Background(), testCase.request, &tfsdk.ValidateAttributeResponse{})

if diff := cmp.Diff(gotSetElems, testCase.expectedSetElems); diff != "" {
t.Errorf("unexpected float64 difference: %s", diff)
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Copypasta typo 🍝

Comment on lines 59 to 61
for _, validator := range v.valueValidators {
validator.Validate(ctx, request, resp)
if resp.Diagnostics.HasError() {
return
}
}
Copy link
Member

Choose a reason for hiding this comment

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

For what its worth (and I think its a good idea, but can be part of a later iteration), helper/validation.All(), which was widely used aggregated errors:

https://github.com/hashicorp/terraform-plugin-sdk/blob/ab572994a48576754ec619691e23bd9afdd7042d/helper/validation/meta.go#L32-L43

I'm having a hard time imagining a case where a "sequential" validation must be performed, as if some later validator was panicking, then it should be fixed to not panic. Giving all diagnostics allows practitioners to fix their Terraform configurations knowing all the rules at once, similar to what @detro is mentioning above in testing, rather than needing to repeatedly validate to find the next issue.

Comment on lines 40 to 46
func AttributeValueTerraformValueDiagnostic(path *tftypes.AttributePath, description string, value string) diag.Diagnostic {
return diag.NewAttributeErrorDiagnostic(
path,
"Invalid Attribute Value Terraform Value",
capitalize(description)+", err: "+value,
)
}
Copy link
Member

Choose a reason for hiding this comment

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

Indeed this should go away with the new attribute path work. My recommendation in the meantime would be prefer messaging that says to contact the provider developers, similar to a lot of what is in the framework today (and hopefully going away more and more as part of hashicorp/terraform-plugin-framework#172).

@bflad bflad added enhancement New feature or request type/set types.Set validators labels Jun 22, 2022
@bendbennett bendbennett merged commit b0eaeea into main Jun 23, 2022
@bendbennett bendbennett deleted the bendbennett/issues-12 branch June 23, 2022 11:33
@github-actions
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 Jul 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request type/set types.Set validators
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Set Element Validation (ValuesAre)
3 participants