From b5bd429e77f25132922cd3fe5dd0f6418d869136 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Fri, 25 Nov 2022 18:11:01 -0500 Subject: [PATCH] website: Updates for validation migrations Reference: https://github.com/hashicorp/terraform-plugin-framework-validators/pull/80 --- .../validators-predefined.mdx | 83 +++++++++---------- .../plugin/framework/path-expressions.mdx | 2 +- 2 files changed, 41 insertions(+), 44 deletions(-) diff --git a/website/docs/plugin/framework/migrating/attributes-blocks/validators-predefined.mdx b/website/docs/plugin/framework/migrating/attributes-blocks/validators-predefined.mdx index efa5245c9..135edaa2c 100644 --- a/website/docs/plugin/framework/migrating/attributes-blocks/validators-predefined.mdx +++ b/website/docs/plugin/framework/migrating/attributes-blocks/validators-predefined.mdx @@ -36,21 +36,20 @@ func resourceExample() *schema.Resource { ``` ## Framework -In the Framework, you implement either type of validation by setting the `Validators` field on the `tfsdk.Attribute` -struct with types that satisfy the `tfsdk.AttributeValidator` interface. Validators that perform the same checks as the -predefined validators in SDKv2 are +In the Framework, you implement either type of validation by setting the `Validators` fields on the attribute +and block types. Validators that perform the same checks as the predefined validators in SDKv2 are [available for the Framework](https://github.com/hashicorp/terraform-plugin-framework-validators). If the predefined validators do not meet your needs, you must define [custom validators](/plugin/framework/migrating/attributes-blocks/validators-custom). ```go -func (d *resourceTypeExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ +func (r *ThingResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { + return schema.Schema{ /* ... */ - Attributes: map[string]tfsdk.Attribute{ - "attribute_example": { - Validators: []tfsdk.AttributeValidator{ - schemavalidator.ConflictsWith( /* ... */ ), + Attributes: map[string]schema.Attribute{ + "attribute_example": schema.StringAttribute{ + Validators: []validator.String{ + stringvalidator.ConflictsWith( /* ... */ ), /* ... */ ``` @@ -76,13 +75,9 @@ Remember the following details when migrating from SDKv2 to the Framework. - In SDKv2, `ValidateDiagFunc` is a field on `schema.Schema` that you can use to define validation functions. In SDKv2, there are also built-in validations. For example, `ConflictsWith` is a field on the `schema.Schema` struct in SDKv2. In -the Framework, `Validators` is a field on each `tfsdk.Attribute` struct. +the Framework, `Validators` is a field on each attribute or block. - Validators replicating the behavior of `ConflictsWith`, `ExactlyOneOf`, `AtLeastOneOf`, and `RequiredWith` in SDKv2 are -available for the Framework: - - [ConflictsWith](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework-validators/schemavalidator#ConflictsWith) - - [ExactlyOneOf](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework-validators/schemavalidator#ExactlyOneOf) - - [AtLeastOneOf](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework-validators/schemavalidator#AtLeastOneOf) - - [AlsoRequires](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework-validators/schemavalidator#AlsoRequires) +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/). - Define [custom validators](/plugin/framework/migrating/attributes-blocks/validators-custom) when the predefined validators do not meet your requirements. @@ -142,38 +137,40 @@ This code implements the `ConflictsWith` and `AlsoRequires` validators with the via the `Validators` field of the provider's `proxy` block's attribute schema. ```go -func (p *provider) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) { - return tfsdk.Schema{ - Blocks: map[string]tfsdk.Block{ - "proxy": { - Attributes: map[string]tfsdk.Attribute{ - "url": { - Validators: []tfsdk.AttributeValidator{ - schemavalidator.ConflictsWith(path.MatchRelative().AtParent().AtName("from_env")), +func (p *ExampleCloudProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) { + resp.Schema = schema.Schema{ + Blocks: map[string]schema.Block{ + "proxy": schema.ListNestedBlock{ + NestedObject: schema.NestedBlockObject{ + Attributes: map[string]tfsdk.Attribute{ + "url": schema.StringAttribute{ + Validators: []validator.String{ + stringvalidator.ConflictsWith(path.MatchRelative().AtParent().AtName("from_env")), + }, + /* ... */ }, - /* ... */ - }, - "username": { - Validators: []tfsdk.AttributeValidator{ - schemavalidator.AlsoRequires(path.MatchRelative().AtParent().AtName("url")), + "username": schema.StringAttribute{ + Validators: []validator.String{ + stringvalidator.AlsoRequires(path.MatchRelative().AtParent().AtName("url")), + }, + /* ... */ }, - /* ... */ - }, - "password": { - Validators: []tfsdk.AttributeValidator{ - schemavalidator.AlsoRequires(path.MatchRelative().AtParent().AtName("username")), + "password": schema.StringAttribute{ + Validators: []validator.String{ + stringvalidator.AlsoRequires(path.MatchRelative().AtParent().AtName("username")), + }, + /* ... */ }, - /* ... */ - }, - "from_env": { - Validators: []tfsdk.AttributeValidator{ - schemavalidator.ConflictsWith( - path.MatchRelative().AtParent().AtName("url"), - path.MatchRelative().AtParent().AtName("username"), - path.MatchRelative().AtParent().AtName("password"), - ), + "from_env": schema.BoolAttribute{ + Validators: []validator.Bool{ + boolvalidator.ConflictsWith( + path.MatchRelative().AtParent().AtName("url"), + path.MatchRelative().AtParent().AtName("username"), + path.MatchRelative().AtParent().AtName("password"), + ), + }, + /* ... */ }, - /* ... */ }, }, }, diff --git a/website/docs/plugin/framework/path-expressions.mdx b/website/docs/plugin/framework/path-expressions.mdx index 09883d426..b52eb1fc4 100644 --- a/website/docs/plugin/framework/path-expressions.mdx +++ b/website/docs/plugin/framework/path-expressions.mdx @@ -16,7 +16,7 @@ Path expressions are logic built on top of [paths](/plugin/framework/paths), whi Example uses include: -- [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). +- [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). Use cases which require exact locations, such as [diagnostics](/plugin/framework/diagnostics), implement [paths](/plugin/framework/paths).