-
Notifications
You must be signed in to change notification settings - Fork 12
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
Introducing schemavalidator
package
#32
Merged
+1,755
−204
Merged
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
189f02b
Marking unused arguments as such
02ff6bf
temporary: attributepath helpers
4a4a7a8
Introducing `schemavalidator` package
4a7c9ab
CHANGELOG entry
190e1e7
Removing any reference to `tftypes.AttributePath` in favour of `path.*`
06801b8
Bump up to latest version of `terraform-plugin-framework`
398db10
Fix changelog entries
d0f4597
Update helpers with a `pathutils` sub-package to help with the new `p…
a5fdeb7
Update all `schemavalidator`s to use `path.Expressions`
e3b6a99
Updating dependencies
c5d5a41
Update .changelog/42.txt
f19e7d0
Apply suggestions from code review
b6d9209
Update schemavalidator/at_least_one_of.go
2d96f2a
PR review feedback
7e4f004
Making the `schemavalidator` permissive when the involved attributes'…
02395ad
Further PR review tweaks
3d5a6f1
Renamed 'RequiredWith' to 'AlsoRequires', and also updated the godoc
ee38042
Making use of the new `.Append()` method added to `path.Paths` and `p…
259abd4
deps: Update github.com/hashicorp/terraform-plugin-framework@main
bflad a357afd
schemavalidator: Updates to use (Expression).MergeExpressions()
bflad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:feature | ||
Introduced `schemavalidator` package with 4 new validation functions: `RequiredWith()`, `ConflictsWith()`, `AtLeastOneOf()`, `ExactlyOneOf()` | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package pathutils | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
) | ||
|
||
// MergeExpressionsWithAttribute returns the given path.Expressions, | ||
// but each has been merged with the given attribute path.Expression, | ||
// and then resolved. | ||
// | ||
// Additionally, if the attribute path.Expression was not part of the initial slice, | ||
// it is added to the result. | ||
func MergeExpressionsWithAttribute(pathExps path.Expressions, attrPathExp path.Expression) path.Expressions { | ||
result := make(path.Expressions, 0, len(pathExps)+1) | ||
|
||
// First, add the attribute own path expression to the result | ||
result = append(result, attrPathExp) | ||
|
||
for _, pe := range pathExps { | ||
mpe := attrPathExp.Merge(pe) | ||
|
||
// Include the merged path expression, | ||
// only if it's not the same as the attribute | ||
if !mpe.Equal(attrPathExp) { | ||
result = append(result, mpe) | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package pathutils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag" | ||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
) | ||
|
||
// PathMatchExpressionsAgainstAttributeConfig returns the path.Paths matching the given path.Expressions. | ||
// | ||
// Each path.Expression has been merged with the given attribute path.Expression | ||
// (likely from the tfsdk.ValidateAttributeRequest), resolved, | ||
// and then matched against the given attribute tfsdk.Config (also from the tfsdk.ValidateAttributeRequest). | ||
// | ||
// This is useful for tfsdk.AttributeValidator that accept path.Expressions, and validate the attributes matching | ||
// to the expressions, in relation to the attribute the validator is applied to. | ||
// For example usage, please look at the `schemavalidator` package in this repository. | ||
func PathMatchExpressionsAgainstAttributeConfig(ctx context.Context, pathExps path.Expressions, attrPathExp path.Expression, attrConfig tfsdk.Config) (path.Paths, diag.Diagnostics) { | ||
var resDiags diag.Diagnostics | ||
|
||
pathExpressions := MergeExpressionsWithAttribute(pathExps, attrPathExp) | ||
|
||
resPaths := make(path.Paths, 0, len(pathExpressions)) | ||
|
||
for _, pe := range pathExpressions { | ||
// Retrieve all the attribute paths that match the given expressions | ||
matchingPaths, diags := attrConfig.PathMatches(ctx, pe) | ||
resDiags.Append(diags...) | ||
if diags.HasError() { | ||
return nil, resDiags | ||
} | ||
|
||
// Confirm at least one attribute was matched. | ||
// If not, collect errors so that the callee can bubble the bugs up. | ||
if len(matchingPaths) == 0 { | ||
resDiags.Append(validatordiag.BugInProviderDiagnostic(fmt.Sprintf("Path expression %q matches no attribute", pe))) | ||
} | ||
|
||
resPaths = append(resPaths, matchingPaths...) | ||
} | ||
|
||
return resPaths, resDiags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Similar to the above comment, this makes me think we should have
(path.Paths).Append()
, if for nothing else but potential deduplication. 😄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.
Totally on board if we want to add that to the types
Paths
andExpressions
. And yes, it should do the extra bit of "deduplicating".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.
Submitted upstream: hashicorp/terraform-plugin-framework#401