Validators - Predefined
Attribute validators ensure that attributes do or do not contain specific values. You can use predefined validators for many use cases, or implement custom validators. Refer to Schemas - Validators in the Framework documentation for details. Refer to the Attributes - Custom Validators page in this guide to learn how to implement custom validators.
The following table describes the mapping between predefined validators in SDKv2 and the Framework. You should use an attribute validator for per-attribute validation, or a data source validator, provider validator or resource validator for declaring validation at the level of the data source, provider or resource, respectively.
SDK Attribute Field | Framework Attribute Validator | Framework Data Source Validator | Framework Provider Validator | Framework Resource Validator |
---|---|---|---|---|
AtLeastOneOf | {TYPE}validator.AtLeastOneOf() | datasourcevalidator.AtLeastOneOf() | providervalidator.AtLeastOneOf() | resourcevalidator.AtLeastOneOf() |
ConflictsWith | {TYPE}validator.ConflictsWith() | datasourcevalidator.Conflicting() | providervalidator.Conflicting() | resourcevalidator.Conflicting() |
ExactlyOneOf | {TYPE}validator.ExactlyOneOf() | datasourcevalidator.ExactlyOneOf() | providervalidator.ExactlyOneOf() | resourcevalidator.ExactlyOneOf() |
RequiredWith | {TYPE}validator.AlsoRequires() | datasourcevalidator.RequiredTogether() | providervalidator.RequiredTogether() | resourcevalidator.RequiredTogether() |
This page explains how to migrate a predefined validator from SDKv2 to the Framework.
SDKv2
In SDKv2, the ConflictsWith
, ExactlyOneOf
, AtLeastOneOf
, and RequiredWith
fields on an attribute's
schema.Schema
struct perform predefined validations on the list of attributes set for these fields.
Framework
In the Framework, you implement either type of validation by setting the Validators
field on the schema.Attribute
implementation. Validators that perform the same checks as the
predefined validators in SDKv2 are
available for the Framework. If the predefined
validators do not meet your needs, you must define
custom validators.
Configuration validators can also be defined for
providers,
resources and
data sources by
implementing ProviderWithConfigValidators
, ResourceWithConfigValidators
, and DataSourceWithConfigValidators
interfaces, respectively.
Migration Notes
Remember the following details when migrating from SDKv2 to the Framework.
- In SDKv2,
ValidateDiagFunc
is a field onschema.Schema
that you can use to define validation functions. In SDKv2, there are also built-in validations. For example,ConflictsWith
is a field on theschema.Schema
struct in SDKv2. In the Framework,Validators
is a field on eachschema.Attribute
implementation. - Validators replicating the behavior of
ConflictsWith
,ExactlyOneOf
,AtLeastOneOf
, andRequiredWith
in SDKv2 are available for the Framework in each of the type-specific packages of terraform-plugin-framework-validators. - Define custom validators when the predefined validators do not meet your requirements.
Example
SDKv2
The following example shows the implementation of the ConflictsWith
field on the
provider's example_block
block's example_attribute_one
attribute.
This validator checks that the provider does not use the example_attribute_one
attribute
when the example_attribute_four
is being used. The example also uses the RequiredWith
field to ensure that the
example_attribute_two
attribute is configured when example_attribute_one
is, and that the
example_attribute_three
attribute is configured when example_attribute_two
is.
Framework
The following shows the same section of provider code after the migration.
This code implements the ConflictsWith
and AlsoRequires
validators with the Framework. The validators are configured
via the Validators
field of the provider's example_block
block's attribute schema.