Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema/validator: New type-specifc validators package #542

Merged
merged 2 commits into from Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}