Skip to content

Commit 8928062

Browse files
authored
all: Migrate implementations to support terraform-plugin-framework version 0.17.0 (#80)
The next terraform-plugin-framework version will have separate `datasource/schema`, `provider/schema`, and `resource/schema` packages which use the type-specific `schema/validator` interfaces. This change updates all schema-based validators to the new interfaces. In the cases where the validators did not match a single type (e.g. `metavalidator`, `schemavalidator` package validators), those validators are now present in all the type-specific validator packages.
1 parent f03f26c commit 8928062

File tree

382 files changed

+15920
-5758
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

382 files changed

+15920
-5758
lines changed

.changelog/80.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
```release-note:breaking-change
2+
all: Migrated implementations to support terraform-plugin-framework version 0.17.0 `datasource/schema`, `provider/schema`, and `resource/schema` packages with type-specific validation
3+
```
4+
5+
```release-note:breaking-change
6+
listvalidator: The `ValuesAre` validator has been removed and split into element type-specific validators in the same package, such as `StringValuesAre`
7+
```
8+
9+
```release-note:breaking-change
10+
mapvalidator: The `ValuesAre` validator has been removed and split into element type-specific validators in the same package, such as `StringValuesAre`
11+
```
12+
13+
```release-note:breaking-change
14+
metavalidator: The `All` and `Any` validators have been removed and split into type-specific packages, such as `stringvalidator.Any`
15+
```
16+
17+
```release-note:breaking-change
18+
schemavalidator: The `AlsoRequires`, `AtLeastOneOf`, `ConflictsWith`, and `ExactlyOneOf` validators have been removed and split into type-specific packages, such as `stringvalidator.ConflictsWith`
19+
```
20+
21+
```release-note:breaking-change
22+
setvalidator: The `ValuesAre` validator has been removed and split into element type-specific validators in the same package, such as `StringValuesAre`
23+
```
24+
25+
```release-note:feature
26+
boolvalidator: New package which contains boolean type specific validators
27+
```
28+
29+
```release-note:feature
30+
objectvalidator: New package which contains object type specific validators
31+
```

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
go.work
2+
go.work.sum
13
# JetBrains IDEs project files
24
.idea/
35
*.iws

boolvalidator/also_requires.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package boolvalidator
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/internal/schemavalidator"
5+
"github.com/hashicorp/terraform-plugin-framework/path"
6+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
7+
)
8+
9+
// AlsoRequires checks that a set of path.Expression has a non-null value,
10+
// if the current attribute or block also has a non-null value.
11+
//
12+
// This implements the validation logic declaratively within the schema.
13+
// Refer to [datasourcevalidator.RequiredTogether],
14+
// [providervalidator.RequiredTogether], or [resourcevalidator.RequiredTogether]
15+
// for declaring this type of validation outside the schema definition.
16+
//
17+
// Relative path.Expression will be resolved using the attribute or block
18+
// being validated.
19+
func AlsoRequires(expressions ...path.Expression) validator.Bool {
20+
return schemavalidator.AlsoRequiresValidator{
21+
PathExpressions: expressions,
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package boolvalidator_test
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
5+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
6+
"github.com/hashicorp/terraform-plugin-framework/path"
7+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
8+
)
9+
10+
func ExampleAlsoRequires() {
11+
// Used within a Schema method of a DataSource, Provider, or Resource
12+
_ = schema.Schema{
13+
Attributes: map[string]schema.Attribute{
14+
"example_attr": schema.BoolAttribute{
15+
Optional: true,
16+
Validators: []validator.Bool{
17+
// Validate this attribute must be configured with other_attr.
18+
boolvalidator.AlsoRequires(path.Expressions{
19+
path.MatchRoot("other_attr"),
20+
}...),
21+
},
22+
},
23+
"other_attr": schema.StringAttribute{
24+
Optional: true,
25+
},
26+
},
27+
}
28+
}

boolvalidator/at_least_one_of.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package boolvalidator
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/internal/schemavalidator"
5+
"github.com/hashicorp/terraform-plugin-framework/path"
6+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
7+
)
8+
9+
// AtLeastOneOf checks that of a set of path.Expression,
10+
// including the attribute this validator is applied to,
11+
// at least one has a non-null value.
12+
//
13+
// This implements the validation logic declaratively within the tfsdk.Schema.
14+
// Refer to [datasourcevalidator.AtLeastOneOf],
15+
// [providervalidator.AtLeastOneOf], or [resourcevalidator.AtLeastOneOf]
16+
// for declaring this type of validation outside the schema definition.
17+
//
18+
// Any relative path.Expression will be resolved using the attribute being
19+
// validated.
20+
func AtLeastOneOf(expressions ...path.Expression) validator.Bool {
21+
return schemavalidator.AtLeastOneOfValidator{
22+
PathExpressions: expressions,
23+
}
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package boolvalidator_test
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
5+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
6+
"github.com/hashicorp/terraform-plugin-framework/path"
7+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
8+
)
9+
10+
func ExampleAtLeastOneOf() {
11+
// Used within a Schema method of a DataSource, Provider, or Resource
12+
_ = schema.Schema{
13+
Attributes: map[string]schema.Attribute{
14+
"example_attr": schema.BoolAttribute{
15+
Optional: true,
16+
Validators: []validator.Bool{
17+
// Validate at least this attribute or other_attr should be configured.
18+
boolvalidator.AtLeastOneOf(path.Expressions{
19+
path.MatchRoot("other_attr"),
20+
}...),
21+
},
22+
},
23+
"other_attr": schema.StringAttribute{
24+
Optional: true,
25+
},
26+
},
27+
}
28+
}

boolvalidator/conflicts_with.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package boolvalidator
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/internal/schemavalidator"
5+
"github.com/hashicorp/terraform-plugin-framework/path"
6+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
7+
)
8+
9+
// ConflictsWith checks that a set of path.Expression,
10+
// including the attribute the validator is applied to,
11+
// do not have a value simultaneously.
12+
//
13+
// This implements the validation logic declaratively within the schema.
14+
// Refer to [datasourcevalidator.Conflicting],
15+
// [providervalidator.Conflicting], or [resourcevalidator.Conflicting]
16+
// for declaring this type of validation outside the schema definition.
17+
//
18+
// Relative path.Expression will be resolved using the attribute being
19+
// validated.
20+
func ConflictsWith(expressions ...path.Expression) validator.Bool {
21+
return schemavalidator.ConflictsWithValidator{
22+
PathExpressions: expressions,
23+
}
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package boolvalidator_test
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
5+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
6+
"github.com/hashicorp/terraform-plugin-framework/path"
7+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
8+
)
9+
10+
func ExampleConflictsWith() {
11+
// Used within a Schema method of a DataSource, Provider, or Resource
12+
_ = schema.Schema{
13+
Attributes: map[string]schema.Attribute{
14+
"example_attr": schema.BoolAttribute{
15+
Optional: true,
16+
Validators: []validator.Bool{
17+
// Validate this attribute must not be configured with other_attr.
18+
boolvalidator.ConflictsWith(path.Expressions{
19+
path.MatchRoot("other_attr"),
20+
}...),
21+
},
22+
},
23+
"other_attr": schema.StringAttribute{
24+
Optional: true,
25+
},
26+
},
27+
}
28+
}

boolvalidator/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package boolvalidator provides validators for types.Bool attributes.
2+
package boolvalidator

boolvalidator/exactly_one_of.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package boolvalidator
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/internal/schemavalidator"
5+
"github.com/hashicorp/terraform-plugin-framework/path"
6+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
7+
)
8+
9+
// ExactlyOneOf checks that of a set of path.Expression,
10+
// including the attribute the validator is applied to,
11+
// one and only one attribute has a value.
12+
// It will also cause a validation error if none are specified.
13+
//
14+
// This implements the validation logic declaratively within the schema.
15+
// Refer to [datasourcevalidator.ExactlyOneOf],
16+
// [providervalidator.ExactlyOneOf], or [resourcevalidator.ExactlyOneOf]
17+
// for declaring this type of validation outside the schema definition.
18+
//
19+
// Relative path.Expression will be resolved using the attribute being
20+
// validated.
21+
func ExactlyOneOf(expressions ...path.Expression) validator.Bool {
22+
return schemavalidator.ExactlyOneOfValidator{
23+
PathExpressions: expressions,
24+
}
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package boolvalidator_test
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
5+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
6+
"github.com/hashicorp/terraform-plugin-framework/path"
7+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
8+
)
9+
10+
func ExampleExactlyOneOf() {
11+
// Used within a Schema method of a DataSource, Provider, or Resource
12+
_ = schema.Schema{
13+
Attributes: map[string]schema.Attribute{
14+
"example_attr": schema.BoolAttribute{
15+
Optional: true,
16+
Validators: []validator.Bool{
17+
// Validate only this attribute or other_attr is configured.
18+
boolvalidator.ExactlyOneOf(path.Expressions{
19+
path.MatchRoot("other_attr"),
20+
}...),
21+
},
22+
},
23+
"other_attr": schema.StringAttribute{
24+
Optional: true,
25+
},
26+
},
27+
}
28+
}

datasourcevalidator/at_least_one_of_test.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"github.com/google/go-cmp/cmp"
88
"github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator"
99
"github.com/hashicorp/terraform-plugin-framework/datasource"
10+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1011
"github.com/hashicorp/terraform-plugin-framework/diag"
1112
"github.com/hashicorp/terraform-plugin-framework/path"
1213
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
13-
"github.com/hashicorp/terraform-plugin-framework/types"
1414
"github.com/hashicorp/terraform-plugin-go/tftypes"
1515
)
1616

@@ -28,15 +28,13 @@ func TestAtLeastOneOf(t *testing.T) {
2828
},
2929
req: datasource.ValidateConfigRequest{
3030
Config: tfsdk.Config{
31-
Schema: tfsdk.Schema{
32-
Attributes: map[string]tfsdk.Attribute{
33-
"test": {
31+
Schema: schema.Schema{
32+
Attributes: map[string]schema.Attribute{
33+
"test": schema.StringAttribute{
3434
Optional: true,
35-
Type: types.StringType,
3635
},
37-
"other": {
36+
"other": schema.StringAttribute{
3837
Optional: true,
39-
Type: types.StringType,
4038
},
4139
},
4240
},
@@ -63,19 +61,16 @@ func TestAtLeastOneOf(t *testing.T) {
6361
},
6462
req: datasource.ValidateConfigRequest{
6563
Config: tfsdk.Config{
66-
Schema: tfsdk.Schema{
67-
Attributes: map[string]tfsdk.Attribute{
68-
"test1": {
64+
Schema: schema.Schema{
65+
Attributes: map[string]schema.Attribute{
66+
"test1": schema.StringAttribute{
6967
Optional: true,
70-
Type: types.StringType,
7168
},
72-
"test2": {
69+
"test2": schema.StringAttribute{
7370
Optional: true,
74-
Type: types.StringType,
7571
},
76-
"other": {
72+
"other": schema.StringAttribute{
7773
Optional: true,
78-
Type: types.StringType,
7974
},
8075
},
8176
},

datasourcevalidator/conflicting_test.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"github.com/google/go-cmp/cmp"
88
"github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator"
99
"github.com/hashicorp/terraform-plugin-framework/datasource"
10+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1011
"github.com/hashicorp/terraform-plugin-framework/diag"
1112
"github.com/hashicorp/terraform-plugin-framework/path"
1213
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
13-
"github.com/hashicorp/terraform-plugin-framework/types"
1414
"github.com/hashicorp/terraform-plugin-go/tftypes"
1515
)
1616

@@ -28,15 +28,13 @@ func TestConflicting(t *testing.T) {
2828
},
2929
req: datasource.ValidateConfigRequest{
3030
Config: tfsdk.Config{
31-
Schema: tfsdk.Schema{
32-
Attributes: map[string]tfsdk.Attribute{
33-
"test": {
31+
Schema: schema.Schema{
32+
Attributes: map[string]schema.Attribute{
33+
"test": schema.StringAttribute{
3434
Optional: true,
35-
Type: types.StringType,
3635
},
37-
"other": {
36+
"other": schema.StringAttribute{
3837
Optional: true,
39-
Type: types.StringType,
4038
},
4139
},
4240
},
@@ -63,19 +61,16 @@ func TestConflicting(t *testing.T) {
6361
},
6462
req: datasource.ValidateConfigRequest{
6563
Config: tfsdk.Config{
66-
Schema: tfsdk.Schema{
67-
Attributes: map[string]tfsdk.Attribute{
68-
"test1": {
64+
Schema: schema.Schema{
65+
Attributes: map[string]schema.Attribute{
66+
"test1": schema.StringAttribute{
6967
Optional: true,
70-
Type: types.StringType,
7168
},
72-
"test2": {
69+
"test2": schema.StringAttribute{
7370
Optional: true,
74-
Type: types.StringType,
7571
},
76-
"other": {
72+
"other": schema.StringAttribute{
7773
Optional: true,
78-
Type: types.StringType,
7974
},
8075
},
8176
},

0 commit comments

Comments
 (0)