Skip to content

Commit

Permalink
schema/validator: New type-specifc validators package (#542)
Browse files Browse the repository at this point in the history
Reference: #83
Reference: #132

As part of upcoming effort to split schema functionality into the `datasource`, `provider`, and `resource` packages, there are some improvements that will land in the new implementations rather than breaking the existing `tfsdk` package schema functionality.

One area which has caused developer burden is that "attribute" validators, currently implementations of the `tfsdk.AttributeValidator` interface,  receive a generic `attr.Value` as the configuration value to perform validation logic. This means that implementors must currently handle validating and converting the value into the concrete type they expect.

The upcoming split schemas handling will introduce separate attribute/block types that will enable to framework to strongly type validators and other future schema enhancements. This change prepares the exported interfaces and internal validation logic for those enhancements.
  • Loading branch information
bflad committed Nov 17, 2022
1 parent 85c0b3f commit b89b71a
Show file tree
Hide file tree
Showing 44 changed files with 5,921 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .changelog/542.txt
@@ -0,0 +1,3 @@
```release-note:feature
schema/validator: New package which contains type-specific schema validator interfaces
```
39 changes: 39 additions & 0 deletions internal/fwschema/attribute.go
Expand Up @@ -74,3 +74,42 @@ type Attribute interface {
// conflict with the tfsdk.Attribute field name.
IsSensitive() bool
}

// AttributesEqual is a helper function to perform equality testing on two
// Attribute. Attribute Equal implementations should still compare the concrete
// types in addition to using this helper.
func AttributesEqual(a, b Attribute) bool {
if !a.GetType().Equal(b.GetType()) {
return false
}

if a.GetDeprecationMessage() != b.GetDeprecationMessage() {
return false
}

if a.GetDescription() != b.GetDescription() {
return false
}

if a.GetMarkdownDescription() != b.GetMarkdownDescription() {
return false
}

if a.IsRequired() != b.IsRequired() {
return false
}

if a.IsOptional() != b.IsOptional() {
return false
}

if a.IsComputed() != b.IsComputed() {
return false
}

if a.IsSensitive() != b.IsSensitive() {
return false
}

return true
}
31 changes: 31 additions & 0 deletions internal/fwschema/block.go
Expand Up @@ -65,3 +65,34 @@ type Block interface {
// Type should return the framework type of a block.
Type() attr.Type
}

// BlocksEqual is a helper function to perform equality testing on two
// Block. Attribute Equal implementations should still compare the concrete
// types in addition to using this helper.
func BlocksEqual(a, b Block) bool {
if !a.Type().Equal(b.Type()) {
return false
}

if a.GetDeprecationMessage() != b.GetDeprecationMessage() {
return false
}

if a.GetDescription() != b.GetDescription() {
return false
}

if a.GetMarkdownDescription() != b.GetMarkdownDescription() {
return false
}

if a.GetMaxItems() != b.GetMaxItems() {
return false
}

if a.GetMinItems() != b.GetMinItems() {
return false
}

return true
}
82 changes: 82 additions & 0 deletions internal/fwschema/fwxschema/attribute_validation.go
Expand Up @@ -2,6 +2,7 @@ package fwxschema

import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)

Expand All @@ -17,3 +18,84 @@ type AttributeWithValidators interface {
// tfsdk.Attribute field name.
GetValidators() []tfsdk.AttributeValidator
}

// AttributeWithBoolValidators is an optional interface on Attribute which
// enables Bool validation support.
type AttributeWithBoolValidators interface {
fwschema.Attribute

// BoolValidators should return a list of Bool validators.
BoolValidators() []validator.Bool
}

// AttributeWithFloat64Validators is an optional interface on Attribute which
// enables Float64 validation support.
type AttributeWithFloat64Validators interface {
fwschema.Attribute

// Float64Validators should return a list of Float64 validators.
Float64Validators() []validator.Float64
}

// AttributeWithInt64Validators is an optional interface on Attribute which
// enables Int64 validation support.
type AttributeWithInt64Validators interface {
fwschema.Attribute

// Int64Validators should return a list of Int64 validators.
Int64Validators() []validator.Int64
}

// AttributeWithListValidators is an optional interface on Attribute which
// enables List validation support.
type AttributeWithListValidators interface {
fwschema.Attribute

// ListValidators should return a list of List validators.
ListValidators() []validator.List
}

// AttributeWithMapValidators is an optional interface on Attribute which
// enables Map validation support.
type AttributeWithMapValidators interface {
fwschema.Attribute

// MapValidators should return a list of Map validators.
MapValidators() []validator.Map
}

// AttributeWithNumberValidators is an optional interface on Attribute which
// enables Number validation support.
type AttributeWithNumberValidators interface {
fwschema.Attribute

// NumberValidators should return a list of Number validators.
NumberValidators() []validator.Number
}

// AttributeWithObjectValidators is an optional interface on Attribute which
// enables Object validation support.
type AttributeWithObjectValidators interface {
fwschema.Attribute

// ObjectValidators should return a list of Object validators.
ObjectValidators() []validator.Object
}

// AttributeWithSetValidators is an optional interface on Attribute which
// enables Set validation support.
type AttributeWithSetValidators interface {
fwschema.Attribute

// SetValidators should return a list of Set validators.
SetValidators() []validator.Set
}

// AttributeWithStringValidators is an optional interface on Attribute which
// enables String validation support.
type AttributeWithStringValidators interface {
fwschema.Attribute

// StringValidators should return a list of String validators.
StringValidators() []validator.String
}
28 changes: 28 additions & 0 deletions internal/fwschema/fwxschema/block_validation.go
Expand Up @@ -2,6 +2,7 @@ package fwxschema

import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)

Expand All @@ -17,3 +18,30 @@ type BlockWithValidators interface {
// tfsdk.Block field name.
GetValidators() []tfsdk.AttributeValidator
}

// BlockWithListValidators is an optional interface on Block which
// enables List validation support.
type BlockWithListValidators interface {
fwschema.Block

// ListValidators should return a list of List validators.
ListValidators() []validator.List
}

// BlockWithObjectValidators is an optional interface on Block which
// enables Object validation support.
type BlockWithObjectValidators interface {
fwschema.Block

// ObjectValidators should return a list of Object validators.
ObjectValidators() []validator.Object
}

// BlockWithSetValidators is an optional interface on Block which
// enables Set validation support.
type BlockWithSetValidators interface {
fwschema.Block

// SetValidators should return a list of Set validators.
SetValidators() []validator.Set
}

0 comments on commit b89b71a

Please sign in to comment.