@@ -10,7 +10,7 @@ description: >-
10
10
-> ** Note:** The Plugin Framework is in beta.
11
11
12
12
Attribute validators ensure that attributes do or do not contain specific values. You can use predefined validators for
13
- many use cases, or implement custom validators. Refer to [ Schemas - Validators] ( /plugin/framework/schemas#validators ) in
13
+ many use cases, or implement custom validators. Refer to [ Schemas - Validators] ( /plugin/framework/handling-data/ schemas#validators ) in
14
14
the Framework documentation for details. Refer to the
15
15
[ Attributes - Custom Validators] ( /plugin/framework/migrating/attributes-blocks/validators-custom ) page in this guide to learn how to
16
16
implement custom validators.
@@ -85,42 +85,40 @@ your requirements.
85
85
86
86
## Example
87
87
88
- The following examples show how to migrate portions of the [tls](https://github.com/hashicorp/terraform-provider-tls)
89
- provider.
90
-
91
- For a complete example, clone the
92
- ` terraform-provider-tls` repository and compare the ` provider.go ` file in
93
- [v3.4.0](https://github.com/hashicorp/terraform-provider-tls/blob/v3.4.0/internal/provider/provider.go)
94
- with [v4.0.1](https://github.com/hashicorp/terraform-provider-tls/blob/v4.0.1/internal/provider/provider.go).
95
-
96
88
### SDKv2
97
89
98
- The following example from the ` provider.go ` file shows the implementation of the ` ConflictsWith` field on the
99
- provider's ` proxy` block's ` url` attribute. This validator checks that the provider does not use the ` url` attribute
100
- when the proxy's url is set through the environment. The example also uses the ` RequiredWith` field to ensure that the
101
- ` password` attribute is configured when ` username` is, and vice-versa.
90
+ The following example shows the implementation of the ` ConflictsWith` field on the
91
+ provider's ` example_block` block's ` example_attribute_one` attribute.
92
+ This validator checks that the provider does not use the ` example_attribute_one` attribute
93
+ when the ` example_attribute_four` is being used. The example also uses the ` RequiredWith` field to ensure that the
94
+ ` example_attribute_two` attribute is configured when ` example_attribute_one` is, and that the
95
+ ` example_attribute_three` attribute is configured when ` example_attribute_two` is.
102
96
103
97
` ` ` go
104
98
func New () (*schema.Provider , error ) {
105
99
return &schema.Provider {
106
100
Schema: map [string ]*schema.Schema {
107
- " proxy " : {
101
+ " example_block " : {
108
102
Elem: &schema.Resource {
109
103
Schema: map [string ]*schema.Schema {
110
- " url " : {
111
- ConflictsWith: []string {" proxy .0.from_env " },
104
+ " example_attribute_one " : {
105
+ ConflictsWith: []string {" example_block .0.example_attribute_four " },
112
106
/* ... */
113
107
},
114
- " username " : {
115
- RequiredWith: []string {" proxy .0.url " },
108
+ " example_attribute_two " : {
109
+ RequiredWith: []string {" example_block .0.example_attribute_one " },
116
110
/* ... */
117
111
},
118
- " password " : {
119
- RequiredWith: []string {" proxy .0.username " },
112
+ " example_attribute_three " : {
113
+ RequiredWith: []string {" example_block .0.example_attribute_two " },
120
114
/* ... */
121
115
},
122
- " from_env" : {
123
- ConflictsWith: []string {" proxy.0.url" , " proxy.0.username" , " proxy.0.password" },
116
+ " example_attribute_four" : {
117
+ ConflictsWith: []string {
118
+ " example_block.0.example_attribute_one" ,
119
+ " example_block.0.example_attribute_two" ,
120
+ " example_block.0.example_attribute_three" ,
121
+ },
124
122
/* ... */
125
123
},
126
124
},
@@ -136,39 +134,39 @@ func New() (*schema.Provider, error) {
136
134
The following shows the same section of provider code after the migration.
137
135
138
136
This code implements the ` ConflictsWith` and ` AlsoRequires` validators with the Framework. The validators are configured
139
- via the ` Validators` field of the provider's ` proxy ` block's attribute schema.
137
+ via the ` Validators` field of the provider's ` example_block ` block's attribute schema.
140
138
141
139
` ` ` go
142
140
func (p *TlsProvider) Schema (_ context.Context , _ provider.SchemaRequest , resp *provider.SchemaResponse ) {
143
141
resp.Schema = schema.Schema {
144
142
Blocks: map [string ]schema.Block {
145
- " proxy " : schema.ListNestedBlock {
143
+ " example_block " : schema.ListNestedBlock {
146
144
NestedObject: schema.NestedBlockObject {
147
145
Attributes: map [string ]schema.Attribute {
148
- " url " : schema.StringAttribute {
146
+ " example_attribute_one " : schema.StringAttribute {
149
147
Validators: []validator.String {
150
- stringvalidator.ConflictsWith (path.MatchRelative ().AtParent ().AtName (" from_env " )),
148
+ stringvalidator.ConflictsWith (path.MatchRelative ().AtParent ().AtName (" example_attribute_four " )),
151
149
},
152
150
/* ... */
153
151
},
154
- " username " : schema.StringAttribute {
152
+ " example_attribute_two " : schema.StringAttribute {
155
153
Validators: []validator.String {
156
- stringvalidator.AlsoRequires (path.MatchRelative ().AtParent ().AtName (" url " )),
154
+ stringvalidator.AlsoRequires (path.MatchRelative ().AtParent ().AtName (" example_attribute_one " )),
157
155
},
158
156
/* ... */
159
157
},
160
- " password " : schema.StringAttribute {
158
+ " example_attribute_three " : schema.StringAttribute {
161
159
Validators: []validator.String {
162
- stringvalidator.AlsoRequires (path.MatchRelative ().AtParent ().AtName (" username " )),
160
+ stringvalidator.AlsoRequires (path.MatchRelative ().AtParent ().AtName (" example_attribute_two " )),
163
161
},
164
162
/* ... */
165
163
},
166
- " from_env " : schema.BoolAttribute {
164
+ " example_attribute_four " : schema.BoolAttribute {
167
165
Validators: []validator.Bool {
168
166
boolvalidator.ConflictsWith (
169
- path.MatchRelative ().AtParent ().AtName (" url " ),
170
- path.MatchRelative ().AtParent ().AtName (" username " ),
171
- path.MatchRelative ().AtParent ().AtName (" password " ),
167
+ path.MatchRelative ().AtParent ().AtName (" example_attribute_one " ),
168
+ path.MatchRelative ().AtParent ().AtName (" example_attribute_two " ),
169
+ path.MatchRelative ().AtParent ().AtName (" example_attribute_three " ),
172
170
),
173
171
},
174
172
/* ... */
0 commit comments