-
Notifications
You must be signed in to change notification settings - Fork 13
Introducing schemavalidator package
#32
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
Changes from 10 commits
189f02b
02ff6bf
4a4a7a8
4a7c9ab
190e1e7
06801b8
398db10
d0f4597
a5fdeb7
e3b6a99
c5d5a41
f19e7d0
b6d9209
2d96f2a
7e4f004
02395ad
3d5a6f1
ee38042
259abd4
a357afd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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()` | ||
| ``` |
| 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).Resolve() | ||
|
|
||
| // 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 | ||
| } | ||
|
detro marked this conversation as resolved.
Outdated
|
||
| 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...) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the above comment, this makes me think we should have
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally on board if we want to add that to the types
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Submitted upstream: hashicorp/terraform-plugin-framework#401 |
||
| } | ||
|
|
||
| return resPaths, resDiags | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.