Skip to content

Commit b5bd429

Browse files
committed
website: Updates for validation migrations
Reference: hashicorp/terraform-plugin-framework-validators#80
1 parent 488e688 commit b5bd429

File tree

2 files changed

+41
-44
lines changed

2 files changed

+41
-44
lines changed

website/docs/plugin/framework/migrating/attributes-blocks/validators-predefined.mdx

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,20 @@ func resourceExample() *schema.Resource {
3636
```
3737
## Framework
3838
39-
In the Framework, you implement either type of validation by setting the `Validators` field on the `tfsdk.Attribute`
40-
struct with types that satisfy the `tfsdk.AttributeValidator` interface. Validators that perform the same checks as the
41-
predefined validators in SDKv2 are
39+
In the Framework, you implement either type of validation by setting the `Validators` fields on the attribute
40+
and block types. Validators that perform the same checks as the predefined validators in SDKv2 are
4241
[available for the Framework](https://github.com/hashicorp/terraform-plugin-framework-validators). If the predefined
4342
validators do not meet your needs, you must define
4443
[custom validators](/plugin/framework/migrating/attributes-blocks/validators-custom).
4544
4645
```go
47-
func (d *resourceTypeExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
48-
return tfsdk.Schema{
46+
func (r *ThingResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
47+
return schema.Schema{
4948
/* ... */
50-
Attributes: map[string]tfsdk.Attribute{
51-
"attribute_example": {
52-
Validators: []tfsdk.AttributeValidator{
53-
schemavalidator.ConflictsWith( /* ... */ ),
49+
Attributes: map[string]schema.Attribute{
50+
"attribute_example": schema.StringAttribute{
51+
Validators: []validator.String{
52+
stringvalidator.ConflictsWith( /* ... */ ),
5453
/* ... */
5554
```
5655
@@ -76,13 +75,9 @@ Remember the following details when migrating from SDKv2 to the Framework.
7675
7776
- In SDKv2, `ValidateDiagFunc` is a field on `schema.Schema` that you can use to define validation functions. In SDKv2,
7877
there are also built-in validations. For example, `ConflictsWith` is a field on the `schema.Schema` struct in SDKv2. In
79-
the Framework, `Validators` is a field on each `tfsdk.Attribute` struct.
78+
the Framework, `Validators` is a field on each attribute or block.
8079
- Validators replicating the behavior of `ConflictsWith`, `ExactlyOneOf`, `AtLeastOneOf`, and `RequiredWith` in SDKv2 are
81-
available for the Framework:
82-
- [ConflictsWith](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework-validators/schemavalidator#ConflictsWith)
83-
- [ExactlyOneOf](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework-validators/schemavalidator#ExactlyOneOf)
84-
- [AtLeastOneOf](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework-validators/schemavalidator#AtLeastOneOf)
85-
- [AlsoRequires](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework-validators/schemavalidator#AlsoRequires)
80+
available for the Framework in each of the [terraform-plugin-framework-validators validator packages](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework-validators/).
8681
- Define [custom validators](/plugin/framework/migrating/attributes-blocks/validators-custom) when the predefined validators do not meet
8782
your requirements.
8883
@@ -142,38 +137,40 @@ This code implements the `ConflictsWith` and `AlsoRequires` validators with the
142137
via the `Validators` field of the provider's `proxy` block's attribute schema.
143138
144139
```go
145-
func (p *provider) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
146-
return tfsdk.Schema{
147-
Blocks: map[string]tfsdk.Block{
148-
"proxy": {
149-
Attributes: map[string]tfsdk.Attribute{
150-
"url": {
151-
Validators: []tfsdk.AttributeValidator{
152-
schemavalidator.ConflictsWith(path.MatchRelative().AtParent().AtName("from_env")),
140+
func (p *ExampleCloudProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
141+
resp.Schema = schema.Schema{
142+
Blocks: map[string]schema.Block{
143+
"proxy": schema.ListNestedBlock{
144+
NestedObject: schema.NestedBlockObject{
145+
Attributes: map[string]tfsdk.Attribute{
146+
"url": schema.StringAttribute{
147+
Validators: []validator.String{
148+
stringvalidator.ConflictsWith(path.MatchRelative().AtParent().AtName("from_env")),
149+
},
150+
/* ... */
153151
},
154-
/* ... */
155-
},
156-
"username": {
157-
Validators: []tfsdk.AttributeValidator{
158-
schemavalidator.AlsoRequires(path.MatchRelative().AtParent().AtName("url")),
152+
"username": schema.StringAttribute{
153+
Validators: []validator.String{
154+
stringvalidator.AlsoRequires(path.MatchRelative().AtParent().AtName("url")),
155+
},
156+
/* ... */
159157
},
160-
/* ... */
161-
},
162-
"password": {
163-
Validators: []tfsdk.AttributeValidator{
164-
schemavalidator.AlsoRequires(path.MatchRelative().AtParent().AtName("username")),
158+
"password": schema.StringAttribute{
159+
Validators: []validator.String{
160+
stringvalidator.AlsoRequires(path.MatchRelative().AtParent().AtName("username")),
161+
},
162+
/* ... */
165163
},
166-
/* ... */
167-
},
168-
"from_env": {
169-
Validators: []tfsdk.AttributeValidator{
170-
schemavalidator.ConflictsWith(
171-
path.MatchRelative().AtParent().AtName("url"),
172-
path.MatchRelative().AtParent().AtName("username"),
173-
path.MatchRelative().AtParent().AtName("password"),
174-
),
164+
"from_env": schema.BoolAttribute{
165+
Validators: []validator.Bool{
166+
boolvalidator.ConflictsWith(
167+
path.MatchRelative().AtParent().AtName("url"),
168+
path.MatchRelative().AtParent().AtName("username"),
169+
path.MatchRelative().AtParent().AtName("password"),
170+
),
171+
},
172+
/* ... */
175173
},
176-
/* ... */
177174
},
178175
},
179176
},

website/docs/plugin/framework/path-expressions.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Path expressions are logic built on top of [paths](/plugin/framework/paths), whi
1616

1717
Example uses include:
1818

19-
- [Path based attribute validators](/plugin/framework/validation#path-based-attribute-validators), such as those in the [`terraform-plugin-framework-validators` module `schemavalidator` package](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/schemavalidator).
19+
- [Path based attribute validators](/plugin/framework/validation#path-based-attribute-validators), such as those in the [`terraform-plugin-framework-validators` module](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework).
2020

2121
Use cases which require exact locations, such as [diagnostics](/plugin/framework/diagnostics), implement [paths](/plugin/framework/paths).
2222

0 commit comments

Comments
 (0)