-
Notifications
You must be signed in to change notification settings - Fork 10
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
Conversation
There was a problem hiding this 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 :)
setvalidator/values_are.go
Outdated
for _, validator := range v.valueValidators { | ||
validator.Validate(ctx, request, resp) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
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.
There was a problem hiding this comment.
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, " + ")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
).
validatordiag/diag.go
Outdated
func AttributeValueTerraformValueDiagnostic(path *tftypes.AttributePath, description string, value string) diag.Diagnostic { | ||
return diag.NewAttributeErrorDiagnostic( | ||
path, | ||
"Invalid Attribute Value Terraform Value", | ||
capitalize(description)+", err: "+value, | ||
) | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have updated the message.
Following discussion with @detro we could consolidate the |
There was a problem hiding this 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 🚀
setvalidator/type_validation_test.go
Outdated
gotSetElems, gotOk := validateSet(context.Background(), testCase.request, &tfsdk.ValidateAttributeResponse{}) | ||
|
||
if diff := cmp.Diff(gotSetElems, testCase.expectedSetElems); diff != "" { | ||
t.Errorf("unexpected float64 difference: %s", diff) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Copypasta typo 🍝
setvalidator/values_are.go
Outdated
for _, validator := range v.valueValidators { | ||
validator.Validate(ctx, request, resp) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} |
There was a problem hiding this comment.
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:
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.
validatordiag/diag.go
Outdated
func AttributeValueTerraformValueDiagnostic(path *tftypes.AttributePath, description string, value string) diag.Diagnostic { | ||
return diag.NewAttributeErrorDiagnostic( | ||
path, | ||
"Invalid Attribute Value Terraform Value", | ||
capitalize(description)+", err: "+value, | ||
) | ||
} |
There was a problem hiding this comment.
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).
2013d59
to
d106fc2
Compare
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. |
Closes: #12