From 46de2c377e713e6b91836287bbdd01c70ac8d326 Mon Sep 17 00:00:00 2001 From: Douglas Harcourt Parsons Date: Tue, 29 Oct 2024 16:45:24 +0000 Subject: [PATCH] Use a validator library instead of custom validation where possible When the TF provider was created, there was no official library for standard validation. This meant that we had to write our own validation functions. This commit replaces those with a library that is maintained by the Terraform team. --- vercel/data_source_project.go | 9 +-- ...data_source_shared_environment_variable.go | 8 ++- vercel/resource_deployment.go | 3 +- vercel/resource_dns_record.go | 26 ++++---- vercel/resource_edge_config.go | 3 +- vercel/resource_edge_config_token.go | 3 +- vercel/resource_log_drain.go | 22 ++++--- vercel/resource_project.go | 39 ++++++----- .../resource_project_deployment_retention.go | 9 +-- vercel/resource_project_domain.go | 3 +- .../resource_project_environment_variable.go | 8 ++- .../resource_shared_environment_variable.go | 8 ++- vercel/resource_team_config.go | 12 ++-- vercel/resource_webhook.go | 8 ++- vercel/validator_float64_greater_than.go | 40 ------------ vercel/validator_float64_less_than.go | 40 ------------ vercel/validator_int64_greater_than.go | 40 ------------ vercel/validator_int64_less_than.go | 40 ------------ vercel/validator_int64_one_of.go | 53 --------------- vercel/validator_map_items_max_count.go | 44 ------------- vercel/validator_map_items_min_count.go | 44 ------------- vercel/validator_map_max_count.go | 42 ------------ vercel/validator_string_length_between.go | 41 ------------ .../validator_string_length_greater_than.go | 42 ------------ vercel/validator_string_one_of.go | 52 --------------- vercel/validator_string_regex.go | 42 ------------ vercel/validator_string_set_items_in.go | 65 ------------------- vercel/validator_string_set_min_count.go | 40 ------------ 28 files changed, 93 insertions(+), 693 deletions(-) delete mode 100644 vercel/validator_float64_greater_than.go delete mode 100644 vercel/validator_float64_less_than.go delete mode 100644 vercel/validator_int64_greater_than.go delete mode 100644 vercel/validator_int64_less_than.go delete mode 100644 vercel/validator_int64_one_of.go delete mode 100644 vercel/validator_map_items_max_count.go delete mode 100644 vercel/validator_map_items_min_count.go delete mode 100644 vercel/validator_map_max_count.go delete mode 100644 vercel/validator_string_length_between.go delete mode 100644 vercel/validator_string_length_greater_than.go delete mode 100644 vercel/validator_string_one_of.go delete mode 100644 vercel/validator_string_regex.go delete mode 100644 vercel/validator_string_set_items_in.go delete mode 100644 vercel/validator_string_set_min_count.go diff --git a/vercel/data_source_project.go b/vercel/data_source_project.go index a6cec9c0..855ec8bd 100644 --- a/vercel/data_source_project.go +++ b/vercel/data_source_project.go @@ -5,6 +5,7 @@ import ( "fmt" "regexp" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" @@ -69,8 +70,8 @@ For more detailed information, please see the [Vercel documentation](https://ver "name": schema.StringAttribute{ Required: true, Validators: []validator.String{ - stringLengthBetween(1, 52), - stringRegex( + stringvalidator.LengthBetween(1, 52), + stringvalidator.RegexMatches( regexp.MustCompile(`^[a-z0-9\-]{0,100}$`), "The name of a Project can only contain up to 100 alphanumeric lowercase characters and hyphens", ), @@ -142,7 +143,7 @@ For more detailed information, please see the [Vercel documentation](https://ver Description: "The git provider of the repository. Must be either `github`, `gitlab`, or `bitbucket`.", Computed: true, Validators: []validator.String{ - stringOneOf("github", "gitlab", "bitbucket"), + stringvalidator.OneOf("github", "gitlab", "bitbucket"), }, }, "repo": schema.StringAttribute{ @@ -237,7 +238,7 @@ For more detailed information, please see the [Vercel documentation](https://ver Computed: true, Optional: true, Validators: []validator.String{ - stringOneOf("team", "global"), + stringvalidator.OneOf("team", "global"), }, }, }, diff --git a/vercel/data_source_shared_environment_variable.go b/vercel/data_source_shared_environment_variable.go index fb6a67fe..e2924ce3 100644 --- a/vercel/data_source_shared_environment_variable.go +++ b/vercel/data_source_shared_environment_variable.go @@ -5,6 +5,8 @@ import ( "fmt" "strings" + "github.com/hashicorp/terraform-plugin-framework-validators/setvalidator" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -108,8 +110,10 @@ For more detailed information, please see the [Vercel documentation](https://ver Description: "The environments that the Environment Variable should be present on. Valid targets are either `production`, `preview`, or `development`.", ElementType: types.StringType, Validators: []validator.Set{ - stringSetItemsIn("production", "preview", "development"), - stringSetMinCount(1), + setvalidator.ValueStringsAre( + stringvalidator.OneOf("production", "preview", "development"), + ), + setvalidator.SizeAtLeast(1), }, }, "key": schema.StringAttribute{ diff --git a/vercel/resource_deployment.go b/vercel/resource_deployment.go index e30ace87..4c8bb10e 100644 --- a/vercel/resource_deployment.go +++ b/vercel/resource_deployment.go @@ -9,6 +9,7 @@ import ( "strconv" "strings" + "github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -128,7 +129,7 @@ terraform to your Deployment. PlanModifiers: []planmodifier.Map{mapplanmodifier.RequiresReplace()}, ElementType: types.StringType, Validators: []validator.Map{ - mapItemsMinCount(1), + mapvalidator.SizeAtLeast(1), }, }, "ref": schema.StringAttribute{ diff --git a/vercel/resource_dns_record.go b/vercel/resource_dns_record.go index d48d36e1..1844b3cc 100644 --- a/vercel/resource_dns_record.go +++ b/vercel/resource_dns_record.go @@ -6,6 +6,8 @@ import ( "strconv" "strings" + "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" @@ -87,7 +89,7 @@ For more detailed information, please see the [Vercel documentation](https://ver PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}, Required: true, Validators: []validator.String{ - stringOneOf("A", "AAAA", "ALIAS", "CAA", "CNAME", "MX", "NS", "SRV", "TXT"), + stringvalidator.OneOf("A", "AAAA", "ALIAS", "CAA", "CNAME", "MX", "NS", "SRV", "TXT"), }, }, "value": schema.StringAttribute{ @@ -100,16 +102,16 @@ For more detailed information, please see the [Vercel documentation](https://ver Optional: true, Computed: true, Validators: []validator.Int64{ - int64GreaterThan(60), - int64LessThan(2147483647), + int64validator.AtLeast(60), + int64validator.AtMost(2147483647), }, }, "mx_priority": schema.Int64Attribute{ Description: "The priority of the MX record. The priority specifies the sequence that an email server receives emails. A smaller value indicates a higher priority.", Optional: true, // required for MX records. Validators: []validator.Int64{ - int64GreaterThan(0), - int64LessThan(65535), + int64validator.AtLeast(0), + int64validator.AtMost(65535), }, }, "comment": schema.StringAttribute{ @@ -118,7 +120,7 @@ For more detailed information, please see the [Vercel documentation](https://ver Computed: true, Default: stringdefault.StaticString(""), Validators: []validator.String{ - stringLengthBetween(0, 500), + stringvalidator.LengthBetween(0, 500), }, }, "srv": schema.SingleNestedAttribute{ @@ -129,24 +131,24 @@ For more detailed information, please see the [Vercel documentation](https://ver Description: "A relative weight for records with the same priority, higher value means higher chance of getting picked.", Required: true, Validators: []validator.Int64{ - int64GreaterThan(0), - int64LessThan(65535), + int64validator.AtLeast(0), + int64validator.AtMost(65535), }, }, "port": schema.Int64Attribute{ Description: "The TCP or UDP port on which the service is to be found.", Required: true, Validators: []validator.Int64{ - int64GreaterThan(0), - int64LessThan(65535), + int64validator.AtLeast(0), + int64validator.AtMost(65535), }, }, "priority": schema.Int64Attribute{ Description: "The priority of the target host, lower value means more preferred.", Required: true, Validators: []validator.Int64{ - int64GreaterThan(0), - int64LessThan(65535), + int64validator.AtLeast(0), + int64validator.AtMost(65535), }, }, "target": schema.StringAttribute{ diff --git a/vercel/resource_edge_config.go b/vercel/resource_edge_config.go index 4be3cc0e..4dced5d0 100644 --- a/vercel/resource_edge_config.go +++ b/vercel/resource_edge_config.go @@ -5,6 +5,7 @@ import ( "fmt" "regexp" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" @@ -64,7 +65,7 @@ An Edge Config is a global data store that enables experimentation with feature Description: "The name/slug of the Edge Config.", Required: true, Validators: []validator.String{ - stringRegex( + stringvalidator.RegexMatches( regexp.MustCompile(`^[a-z0-9\_\-]{0,32}$`), "The name of an Edge Config can only contain up to 32 alphanumeric lowercase characters, hyphens and underscores.", ), diff --git a/vercel/resource_edge_config_token.go b/vercel/resource_edge_config_token.go index 74aad43a..a7bdbf50 100644 --- a/vercel/resource_edge_config_token.go +++ b/vercel/resource_edge_config_token.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" @@ -67,7 +68,7 @@ An Edge Config token is used to authenticate against an Edge Config's endpoint. Required: true, PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}, Validators: []validator.String{ - stringLengthBetween(1, 52), + stringvalidator.LengthBetween(1, 52), }, }, "edge_config_id": schema.StringAttribute{ diff --git a/vercel/resource_log_drain.go b/vercel/resource_log_drain.go index 5339a85f..fc9b4155 100644 --- a/vercel/resource_log_drain.go +++ b/vercel/resource_log_drain.go @@ -4,6 +4,10 @@ import ( "context" "fmt" + "github.com/hashicorp/terraform-plugin-framework-validators/float64validator" + "github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator" + "github.com/hashicorp/terraform-plugin-framework-validators/setvalidator" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -84,7 +88,7 @@ Teams on Pro and Enterprise plans can subscribe to log drains that are generic a Required: true, PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}, Validators: []validator.String{ - stringOneOf("json", "ndjson"), + stringvalidator.OneOf("json", "ndjson"), }, }, "environments": schema.SetAttribute{ @@ -93,8 +97,8 @@ Teams on Pro and Enterprise plans can subscribe to log drains that are generic a PlanModifiers: []planmodifier.Set{setplanmodifier.RequiresReplace()}, Required: true, Validators: []validator.Set{ - stringSetItemsIn("production", "preview"), - stringSetMinCount(1), + setvalidator.ValueStringsAre(stringvalidator.OneOf("production", "preview")), + setvalidator.SizeAtLeast(1), }, }, "headers": schema.MapAttribute{ @@ -103,7 +107,7 @@ Teams on Pro and Enterprise plans can subscribe to log drains that are generic a PlanModifiers: []planmodifier.Map{mapplanmodifier.RequiresReplace()}, Optional: true, Validators: []validator.Map{ - mapMaxCount(5), + mapvalidator.SizeAtMost(5), }, }, "project_ids": schema.SetAttribute{ @@ -117,8 +121,8 @@ Teams on Pro and Enterprise plans can subscribe to log drains that are generic a Optional: true, PlanModifiers: []planmodifier.Float64{float64planmodifier.RequiresReplace()}, Validators: []validator.Float64{ - float64GreaterThan(0), - float64LessThan(1), + float64validator.AtLeast(0), + float64validator.AtMost(1), }, }, "secret": schema.StringAttribute{ @@ -128,7 +132,7 @@ Teams on Pro and Enterprise plans can subscribe to log drains that are generic a Sensitive: true, PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplaceIfConfigured(), stringplanmodifier.UseStateForUnknown()}, Validators: []validator.String{ - stringLengthGreaterThan(32), + stringvalidator.LengthAtLeast(32), }, }, "sources": schema.SetAttribute{ @@ -137,8 +141,8 @@ Teams on Pro and Enterprise plans can subscribe to log drains that are generic a ElementType: types.StringType, PlanModifiers: []planmodifier.Set{setplanmodifier.RequiresReplace()}, Validators: []validator.Set{ - stringSetItemsIn("static", "edge", "external", "build", "lambda"), - stringSetMinCount(1), + setvalidator.ValueStringsAre(stringvalidator.OneOf("static", "edge", "external", "build", "lambda")), + setvalidator.SizeAtLeast(1), }, }, "endpoint": schema.StringAttribute{ diff --git a/vercel/resource_project.go b/vercel/resource_project.go index bd3fe5f2..ceaedf37 100644 --- a/vercel/resource_project.go +++ b/vercel/resource_project.go @@ -5,6 +5,9 @@ import ( "fmt" "regexp" + "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" + "github.com/hashicorp/terraform-plugin-framework-validators/setvalidator" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/path" @@ -86,8 +89,8 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ "name": schema.StringAttribute{ Required: true, Validators: []validator.String{ - stringLengthBetween(1, 52), - stringRegex( + stringvalidator.LengthBetween(1, 52), + stringvalidator.RegexMatches( regexp.MustCompile(`^[a-z0-9\-]{0,100}$`), "The name of a Project can only contain up to 100 alphanumeric lowercase characters and hyphens", ), @@ -124,8 +127,8 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ Description: "The environments that the Environment Variable should be present on. Valid targets are either `production`, `preview`, or `development`.", ElementType: types.StringType, Validators: []validator.Set{ - stringSetItemsIn("production", "preview", "development"), - stringSetMinCount(1), + setvalidator.ValueStringsAre(stringvalidator.OneOf("production", "preview", "development")), + setvalidator.SizeAtLeast(1), }, Required: true, }, @@ -156,7 +159,7 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ Optional: true, Computed: true, Validators: []validator.String{ - stringLengthBetween(0, 1000), + stringvalidator.LengthBetween(0, 1000), }, }, }, @@ -178,7 +181,7 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ Description: "The git provider of the repository. Must be either `github`, `gitlab`, or `bitbucket`.", Required: true, Validators: []validator.String{ - stringOneOf("github", "gitlab", "bitbucket"), + stringvalidator.OneOf("github", "gitlab", "bitbucket"), }, }, "repo": schema.StringAttribute{ @@ -237,7 +240,7 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ Description: "The deployment environment to protect. Must be one of `standard_protection`, `all_deployments`, `only_preview_deployments`, or `none`.", PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, Validators: []validator.String{ - stringOneOf("standard_protection", "all_deployments", "only_preview_deployments", "none"), + stringvalidator.OneOf("standard_protection", "all_deployments", "only_preview_deployments", "none"), }, }, }, @@ -251,7 +254,7 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ Required: true, Sensitive: true, Validators: []validator.String{ - stringLengthBetween(1, 72), + stringvalidator.LengthBetween(1, 72), }, }, "deployment_type": schema.StringAttribute{ @@ -259,7 +262,7 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ Description: "The deployment environment to protect. Must be one of `standard_protection`, `all_deployments`, or `only_preview_deployments`.", PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, Validators: []validator.String{ - stringOneOf("standard_protection", "all_deployments", "only_preview_deployments"), + stringvalidator.OneOf("standard_protection", "all_deployments", "only_preview_deployments"), }, }, }, @@ -286,7 +289,7 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ }, }, Validators: []validator.Set{ - stringSetMinCount(1), + setvalidator.SizeAtLeast(1), }, }, "deployment_type": schema.StringAttribute{ @@ -294,7 +297,7 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ Description: "The deployment environment to protect. Must be one of `standard_protection`, `all_deployments`, `only_production_deployments`, or `only_preview_deployments`.", PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, Validators: []validator.String{ - stringOneOf("standard_protection", "all_deployments", "only_production_deployments", "only_preview_deployments"), + stringvalidator.OneOf("standard_protection", "all_deployments", "only_production_deployments", "only_preview_deployments"), }, }, "protection_mode": schema.StringAttribute{ @@ -304,7 +307,7 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ Description: "Whether or not Trusted IPs is optional to access a deployment. Must be either `trusted_ip_required` or `trusted_ip_optional`. `trusted_ip_optional` is only available with Standalone Trusted IPs.", PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, Validators: []validator.String{ - stringOneOf("trusted_ip_required", "trusted_ip_optional"), + stringvalidator.OneOf("trusted_ip_required", "trusted_ip_optional"), }, }, }, @@ -325,7 +328,7 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ Description: "Configures the URL of the `iss` claim. `team` = `https://oidc.vercel.com/[team_slug]` `global` = `https://oidc.vercel.com`", PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, Validators: []validator.String{ - stringOneOf("team", "global"), + stringvalidator.OneOf("team", "global"), }, }, }, @@ -357,7 +360,7 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ }, }, Validators: []validator.Set{ - stringSetMinCount(1), + setvalidator.SizeAtLeast(1), }, }, }, @@ -461,7 +464,7 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ Optional: true, Description: "Ensures that outdated clients always fetch the correct version for a given deployment. This value defines how long Vercel keeps Skew Protection active.", Validators: []validator.String{ - stringOneOf("30 minutes", "12 hours", "1 day", "7 days"), + stringvalidator.OneOf("30 minutes", "12 hours", "1 day", "7 days"), }, }, "resource_config": schema.SingleNestedAttribute{ @@ -475,7 +478,7 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ Optional: true, Computed: true, Validators: []validator.String{ - stringOneOf("standard_legacy", "standard", "performance"), + stringvalidator.OneOf("standard_legacy", "standard", "performance"), }, PlanModifiers: []planmodifier.String{SuppressDiffIfNotConfigured(), stringplanmodifier.UseStateForUnknown()}, }, @@ -484,8 +487,8 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ Optional: true, Computed: true, Validators: []validator.Int64{ - int64GreaterThan(0), - int64LessThan(901), + int64validator.AtLeast(0), + int64validator.AtMost(901), }, PlanModifiers: []planmodifier.Int64{SuppressDiffIfNotConfigured(), int64planmodifier.UseStateForUnknown()}, }, diff --git a/vercel/resource_project_deployment_retention.go b/vercel/resource_project_deployment_retention.go index bee5733c..a43160ca 100644 --- a/vercel/resource_project_deployment_retention.go +++ b/vercel/resource_project_deployment_retention.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" @@ -68,7 +69,7 @@ For more detailed information, please see the [Vercel documentation](https://ver Description: "The retention period for preview deployments. Should be one of '1m', '2m', '3m', '6m', '1y', 'unlimited'.", Default: stringdefault.StaticString("unlimited"), Validators: []validator.String{ - stringOneOf("1m", "2m", "3m", "6m", "1y", "unlimited"), + stringvalidator.OneOf("1m", "2m", "3m", "6m", "1y", "unlimited"), }, }, "expiration_production": schema.StringAttribute{ @@ -77,7 +78,7 @@ For more detailed information, please see the [Vercel documentation](https://ver Description: "The retention period for production deployments. Should be one of '1m', '2m', '3m', '6m', '1y', 'unlimited'.", Default: stringdefault.StaticString("unlimited"), Validators: []validator.String{ - stringOneOf("1m", "2m", "3m", "6m", "1y", "unlimited"), + stringvalidator.OneOf("1m", "2m", "3m", "6m", "1y", "unlimited"), }, }, "expiration_canceled": schema.StringAttribute{ @@ -86,7 +87,7 @@ For more detailed information, please see the [Vercel documentation](https://ver Description: "The retention period for canceled deployments. Should be one of '1m', '2m', '3m', '6m', '1y', 'unlimited'.", Default: stringdefault.StaticString("unlimited"), Validators: []validator.String{ - stringOneOf("1m", "2m", "3m", "6m", "1y", "unlimited"), + stringvalidator.OneOf("1m", "2m", "3m", "6m", "1y", "unlimited"), }, }, "expiration_errored": schema.StringAttribute{ @@ -95,7 +96,7 @@ For more detailed information, please see the [Vercel documentation](https://ver Description: "The retention period for errored deployments. Should be one of '1m', '2m', '3m', '6m', '1y', 'unlimited'.", Default: stringdefault.StaticString("unlimited"), Validators: []validator.String{ - stringOneOf("1m", "2m", "3m", "6m", "1y", "unlimited"), + stringvalidator.OneOf("1m", "2m", "3m", "6m", "1y", "unlimited"), }, }, "project_id": schema.StringAttribute{ diff --git a/vercel/resource_project_domain.go b/vercel/resource_project_domain.go index 49d42395..fda92518 100644 --- a/vercel/resource_project_domain.go +++ b/vercel/resource_project_domain.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" @@ -87,7 +88,7 @@ By default, Project Domains will be automatically applied to any ` + "`productio Description: "The HTTP status code to use when serving as a redirect.", Optional: true, Validators: []validator.Int64{ - int64OneOf(301, 302, 307, 308), + int64validator.OneOf(301, 302, 307, 308), }, }, "git_branch": schema.StringAttribute{ diff --git a/vercel/resource_project_environment_variable.go b/vercel/resource_project_environment_variable.go index d3ceff68..8032c6b5 100644 --- a/vercel/resource_project_environment_variable.go +++ b/vercel/resource_project_environment_variable.go @@ -4,6 +4,8 @@ import ( "context" "fmt" + "github.com/hashicorp/terraform-plugin-framework-validators/setvalidator" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -72,8 +74,8 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ Description: "The environments that the Environment Variable should be present on. Valid targets are either `production`, `preview`, or `development`.", ElementType: types.StringType, Validators: []validator.Set{ - stringSetItemsIn("production", "preview", "development"), - stringSetMinCount(1), + setvalidator.ValueStringsAre(stringvalidator.OneOf("production", "preview", "development")), + setvalidator.SizeAtLeast(1), }, }, "key": schema.StringAttribute{ @@ -118,7 +120,7 @@ At this time you cannot use a Vercel Project resource with in-line ` + "`environ Optional: true, Computed: true, Validators: []validator.String{ - stringLengthBetween(0, 1000), + stringvalidator.LengthBetween(0, 1000), }, }, }, diff --git a/vercel/resource_shared_environment_variable.go b/vercel/resource_shared_environment_variable.go index 99da23bf..f369e43d 100644 --- a/vercel/resource_shared_environment_variable.go +++ b/vercel/resource_shared_environment_variable.go @@ -4,6 +4,8 @@ import ( "context" "fmt" + "github.com/hashicorp/terraform-plugin-framework-validators/setvalidator" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/path" @@ -114,8 +116,8 @@ For more detailed information, please see the [Vercel documentation](https://ver Description: "The environments that the Environment Variable should be present on. Valid targets are either `production`, `preview`, or `development`.", ElementType: types.StringType, Validators: []validator.Set{ - stringSetItemsIn("production", "preview", "development"), - stringSetMinCount(1), + setvalidator.ValueStringsAre(stringvalidator.OneOf("production", "preview", "development")), + setvalidator.SizeAtLeast(1), }, }, "key": schema.StringAttribute{ @@ -155,7 +157,7 @@ For more detailed information, please see the [Vercel documentation](https://ver Optional: true, Computed: true, Validators: []validator.String{ - stringLengthBetween(0, 1000), + stringvalidator.LengthBetween(0, 1000), }, }, }, diff --git a/vercel/resource_team_config.go b/vercel/resource_team_config.go index 8ba30389..0149b158 100644 --- a/vercel/resource_team_config.go +++ b/vercel/resource_team_config.go @@ -93,8 +93,8 @@ func (r *teamConfigResource) Schema(_ context.Context, req resource.SchemaReques PlanModifiers: []planmodifier.Map{mapplanmodifier.RequiresReplace()}, ElementType: types.StringType, Validators: []validator.Map{ - mapItemsMinCount(1), - mapItemsMaxCount(1), + mapvalidator.SizeAtLeast(1), + mapvalidator.SizeAtMost(1), }, }, "description": schema.StringAttribute{ @@ -108,7 +108,7 @@ func (r *teamConfigResource) Schema(_ context.Context, req resource.SchemaReques Computed: true, PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, Validators: []validator.String{ - stringOneOf("on", "off"), + stringvalidator.OneOf("on", "off"), }, }, "email_domain": schema.StringAttribute{ @@ -138,7 +138,7 @@ func (r *teamConfigResource) Schema(_ context.Context, req resource.SchemaReques Description: "The ID of the access group to use for the team.", Optional: true, Validators: []validator.String{ - stringRegex(regexp.MustCompile("^ag_[A-z0-9_ -]+$"), "Access group ID must be a valid access group"), + stringvalidator.RegexMatches(regexp.MustCompile("^ag_[A-z0-9_ -]+$"), "Access group ID must be a valid access group"), // Validate only this attribute or roles is configured. stringvalidator.ExactlyOneOf(path.Expressions{ path.MatchRoot("saml.roles"), @@ -181,7 +181,7 @@ func (r *teamConfigResource) Schema(_ context.Context, req resource.SchemaReques Computed: true, PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, Validators: []validator.String{ - stringOneOf("default", "on", "off"), + stringvalidator.OneOf("default", "on", "off"), }, }, "enable_production_feedback": schema.StringAttribute{ @@ -189,7 +189,7 @@ func (r *teamConfigResource) Schema(_ context.Context, req resource.SchemaReques Computed: true, PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, Validators: []validator.String{ - stringOneOf("default", "on", "off"), + stringvalidator.OneOf("default", "on", "off"), }, }, "hide_ip_addresses": schema.BoolAttribute{ diff --git a/vercel/resource_webhook.go b/vercel/resource_webhook.go index 1b0a2112..2624be2c 100644 --- a/vercel/resource_webhook.go +++ b/vercel/resource_webhook.go @@ -4,6 +4,8 @@ import ( "context" "fmt" + "github.com/hashicorp/terraform-plugin-framework-validators/setvalidator" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" @@ -78,15 +80,15 @@ When an event happens, a webhook is sent to a third-party app, which can then ta Required: true, ElementType: types.StringType, Validators: []validator.Set{ - stringSetItemsIn( + setvalidator.ValueStringsAre(stringvalidator.OneOf( "deployment.created", "deployment.error", "deployment.canceled", "deployment.succeeded", "project.created", "project.removed", - ), - stringSetMinCount(1), + )), + setvalidator.SizeAtLeast(1), }, PlanModifiers: []planmodifier.Set{setplanmodifier.RequiresReplace()}, }, diff --git a/vercel/validator_float64_greater_than.go b/vercel/validator_float64_greater_than.go deleted file mode 100644 index cc843513..00000000 --- a/vercel/validator_float64_greater_than.go +++ /dev/null @@ -1,40 +0,0 @@ -package vercel - -import ( - "context" - "fmt" - - "github.com/hashicorp/terraform-plugin-framework/schema/validator" -) - -func float64GreaterThan(val float64) validatorFloat64GreaterThan { - return validatorFloat64GreaterThan{ - Min: val, - } -} - -type validatorFloat64GreaterThan struct { - Min float64 -} - -func (v validatorFloat64GreaterThan) Description(ctx context.Context) string { - return fmt.Sprintf("Value must be equal to or greater than %.2f", v.Min) -} -func (v validatorFloat64GreaterThan) MarkdownDescription(ctx context.Context) string { - return fmt.Sprintf("Value must be equal to or greater than `%.2f`", v.Min) -} - -func (v validatorFloat64GreaterThan) ValidateFloat64(ctx context.Context, req validator.Float64Request, resp *validator.Float64Response) { - if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() { - return - } - - if req.ConfigValue.ValueFloat64() < v.Min { - resp.Diagnostics.AddAttributeError( - req.Path, - "Invalid value provided", - fmt.Sprintf("Value must be greater than %.2f, got: %.2f.", v.Min, req.ConfigValue.ValueFloat64()), - ) - return - } -} diff --git a/vercel/validator_float64_less_than.go b/vercel/validator_float64_less_than.go deleted file mode 100644 index 5a568fa9..00000000 --- a/vercel/validator_float64_less_than.go +++ /dev/null @@ -1,40 +0,0 @@ -package vercel - -import ( - "context" - "fmt" - - "github.com/hashicorp/terraform-plugin-framework/schema/validator" -) - -func float64LessThan(val float64) validatorFloat64LessThan { - return validatorFloat64LessThan{ - Max: val, - } -} - -type validatorFloat64LessThan struct { - Max float64 -} - -func (v validatorFloat64LessThan) Description(ctx context.Context) string { - return fmt.Sprintf("Value must be equal to or less than %.2f", v.Max) -} -func (v validatorFloat64LessThan) MarkdownDescription(ctx context.Context) string { - return fmt.Sprintf("Value must be equal to or less than `%.2f`", v.Max) -} - -func (v validatorFloat64LessThan) ValidateFloat64(ctx context.Context, req validator.Float64Request, resp *validator.Float64Response) { - if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() { - return - } - - if req.ConfigValue.ValueFloat64() > v.Max { - resp.Diagnostics.AddAttributeError( - req.Path, - "Invalid value provided", - fmt.Sprintf("Value must be less than %.2f, got: %.2f.", v.Max, req.ConfigValue.ValueFloat64()), - ) - return - } -} diff --git a/vercel/validator_int64_greater_than.go b/vercel/validator_int64_greater_than.go deleted file mode 100644 index 51e99616..00000000 --- a/vercel/validator_int64_greater_than.go +++ /dev/null @@ -1,40 +0,0 @@ -package vercel - -import ( - "context" - "fmt" - - "github.com/hashicorp/terraform-plugin-framework/schema/validator" -) - -func int64GreaterThan(val int64) validatorInt64GreaterThan { - return validatorInt64GreaterThan{ - Min: val, - } -} - -type validatorInt64GreaterThan struct { - Min int64 -} - -func (v validatorInt64GreaterThan) Description(ctx context.Context) string { - return fmt.Sprintf("Value must be equal to or greater than %d", v.Min) -} -func (v validatorInt64GreaterThan) MarkdownDescription(ctx context.Context) string { - return fmt.Sprintf("Value must be equal to or greater than `%d`", v.Min) -} - -func (v validatorInt64GreaterThan) ValidateInt64(ctx context.Context, req validator.Int64Request, resp *validator.Int64Response) { - if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() { - return - } - - if req.ConfigValue.ValueInt64() < v.Min { - resp.Diagnostics.AddAttributeError( - req.Path, - "Invalid value provided", - fmt.Sprintf("Value must be greater than %d, got: %d.", v.Min, req.ConfigValue.ValueInt64()), - ) - return - } -} diff --git a/vercel/validator_int64_less_than.go b/vercel/validator_int64_less_than.go deleted file mode 100644 index 7d51c847..00000000 --- a/vercel/validator_int64_less_than.go +++ /dev/null @@ -1,40 +0,0 @@ -package vercel - -import ( - "context" - "fmt" - - "github.com/hashicorp/terraform-plugin-framework/schema/validator" -) - -func int64LessThan(val int64) validatorInt64LessThan { - return validatorInt64LessThan{ - Max: val, - } -} - -type validatorInt64LessThan struct { - Max int64 -} - -func (v validatorInt64LessThan) Description(ctx context.Context) string { - return fmt.Sprintf("Value must be equal to or less than %d", v.Max) -} -func (v validatorInt64LessThan) MarkdownDescription(ctx context.Context) string { - return fmt.Sprintf("Value must be equal to or less than `%d`", v.Max) -} - -func (v validatorInt64LessThan) ValidateInt64(ctx context.Context, req validator.Int64Request, resp *validator.Int64Response) { - if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() { - return - } - - if req.ConfigValue.ValueInt64() > v.Max { - resp.Diagnostics.AddAttributeError( - req.Path, - "Invalid value provided", - fmt.Sprintf("Value must be less than %d, got: %d.", v.Max, req.ConfigValue.ValueInt64()), - ) - return - } -} diff --git a/vercel/validator_int64_one_of.go b/vercel/validator_int64_one_of.go deleted file mode 100644 index fdc9e67b..00000000 --- a/vercel/validator_int64_one_of.go +++ /dev/null @@ -1,53 +0,0 @@ -package vercel - -import ( - "context" - "fmt" - "strconv" - "strings" - - "github.com/hashicorp/terraform-plugin-framework/schema/validator" -) - -func int64OneOf(items ...int64) validatorInt64OneOf { - itemMap := map[int64]struct{}{} - for _, i := range items { - itemMap[i] = struct{}{} - } - return validatorInt64OneOf{ - Items: itemMap, - } -} - -type validatorInt64OneOf struct { - Items map[int64]struct{} -} - -func (v validatorInt64OneOf) keys() (out []string) { - for k := range v.Items { - out = append(out, strconv.Itoa(int(k))) - } - return -} - -func (v validatorInt64OneOf) Description(ctx context.Context) string { - return fmt.Sprintf("Item must be one of %s", strings.Join(v.keys(), " ")) -} -func (v validatorInt64OneOf) MarkdownDescription(ctx context.Context) string { - return fmt.Sprintf("Item must be one of `%s`", strings.Join(v.keys(), "` `")) -} - -func (v validatorInt64OneOf) ValidateInt64(ctx context.Context, req validator.Int64Request, resp *validator.Int64Response) { - if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() { - return - } - - if _, ok := v.Items[req.ConfigValue.ValueInt64()]; !ok { - resp.Diagnostics.AddAttributeError( - req.Path, - "Invalid value provided", - fmt.Sprintf("Item must be one of %s, got: %d.", strings.Join(v.keys(), " "), req.ConfigValue.ValueInt64()), - ) - return - } -} diff --git a/vercel/validator_map_items_max_count.go b/vercel/validator_map_items_max_count.go deleted file mode 100644 index c9f17757..00000000 --- a/vercel/validator_map_items_max_count.go +++ /dev/null @@ -1,44 +0,0 @@ -package vercel - -import ( - "context" - "fmt" - - "github.com/hashicorp/terraform-plugin-framework/schema/validator" -) - -func mapItemsMaxCount(minCount int) validatorMapItemsMaxCount { - return validatorMapItemsMaxCount{ - Max: minCount, - } -} - -type validatorMapItemsMaxCount struct { - Max int -} - -func (v validatorMapItemsMaxCount) Description(ctx context.Context) string { - return fmt.Sprintf("Map must contain %d or more item(s)", v.Max) -} -func (v validatorMapItemsMaxCount) MarkdownDescription(ctx context.Context) string { - return fmt.Sprintf("Map must contain %d or more item(s)", v.Max) -} - -func (v validatorMapItemsMaxCount) ValidateMap(ctx context.Context, req validator.MapRequest, resp *validator.MapResponse) { - if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() { - return - } - count := len(req.ConfigValue.Elements()) - if count > v.Max { - resp.Diagnostics.AddAttributeError( - req.Path, - "Invalid value provided", - fmt.Sprintf( - "Map must contain no more than %d items, got: %d.", - v.Max, - count, - ), - ) - return - } -} diff --git a/vercel/validator_map_items_min_count.go b/vercel/validator_map_items_min_count.go deleted file mode 100644 index 1456d0cd..00000000 --- a/vercel/validator_map_items_min_count.go +++ /dev/null @@ -1,44 +0,0 @@ -package vercel - -import ( - "context" - "fmt" - - "github.com/hashicorp/terraform-plugin-framework/schema/validator" -) - -func mapItemsMinCount(minCount int) validatorMapItemsMinCount { - return validatorMapItemsMinCount{ - Min: minCount, - } -} - -type validatorMapItemsMinCount struct { - Min int -} - -func (v validatorMapItemsMinCount) Description(ctx context.Context) string { - return fmt.Sprintf("Map must contain %d or more item(s)", v.Min) -} -func (v validatorMapItemsMinCount) MarkdownDescription(ctx context.Context) string { - return fmt.Sprintf("Map must contain %d or more item(s)", v.Min) -} - -func (v validatorMapItemsMinCount) ValidateMap(ctx context.Context, req validator.MapRequest, resp *validator.MapResponse) { - if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() { - return - } - count := len(req.ConfigValue.Elements()) - if count < v.Min { - resp.Diagnostics.AddAttributeError( - req.Path, - "Invalid value provided", - fmt.Sprintf( - "Map must contain at least %d items, got: %d.", - v.Min, - count, - ), - ) - return - } -} diff --git a/vercel/validator_map_max_count.go b/vercel/validator_map_max_count.go deleted file mode 100644 index 4529bd72..00000000 --- a/vercel/validator_map_max_count.go +++ /dev/null @@ -1,42 +0,0 @@ -package vercel - -import ( - "context" - "fmt" - - "github.com/hashicorp/terraform-plugin-framework/schema/validator" -) - -var _ validator.Map = validatorMapMaxCount{} - -func mapMaxCount(max int) validatorMapMaxCount { - return validatorMapMaxCount{ - Max: max, - } -} - -type validatorMapMaxCount struct { - Max int -} - -func (v validatorMapMaxCount) Description(ctx context.Context) string { - return fmt.Sprintf("Map must contain %d or fewer items", v.Max) -} -func (v validatorMapMaxCount) MarkdownDescription(ctx context.Context) string { - return fmt.Sprintf("Map must contain %d or fewer items", v.Max) -} - -func (v validatorMapMaxCount) ValidateMap(ctx context.Context, req validator.MapRequest, resp *validator.MapResponse) { - if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() { - return - } - - if len(req.ConfigValue.Elements()) > v.Max { - resp.Diagnostics.AddAttributeError( - req.Path, - "Invalid value provided", - v.Description(ctx), - ) - return - } -} diff --git a/vercel/validator_string_length_between.go b/vercel/validator_string_length_between.go deleted file mode 100644 index 142e470b..00000000 --- a/vercel/validator_string_length_between.go +++ /dev/null @@ -1,41 +0,0 @@ -package vercel - -import ( - "context" - "fmt" - - "github.com/hashicorp/terraform-plugin-framework/schema/validator" -) - -func stringLengthBetween(minLength int, maxLength int) validatorStringLengthBetween { - return validatorStringLengthBetween{ - Max: maxLength, - Min: minLength, - } -} - -type validatorStringLengthBetween struct { - Max int - Min int -} - -func (v validatorStringLengthBetween) Description(ctx context.Context) string { - return fmt.Sprintf("String length must be between %d and %d", v.Min, v.Max) -} -func (v validatorStringLengthBetween) MarkdownDescription(ctx context.Context) string { - return fmt.Sprintf("String length must be between `%d` and `%d`", v.Min, v.Max) -} - -func (v validatorStringLengthBetween) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) { - if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() { - return - } - strLen := len(req.ConfigValue.ValueString()) - if strLen < v.Min || strLen > v.Max { - resp.Diagnostics.AddError( - "Invalid value provided", - fmt.Sprintf("String length must be between %d and %d, got: %d.", v.Min, v.Max, strLen), - ) - return - } -} diff --git a/vercel/validator_string_length_greater_than.go b/vercel/validator_string_length_greater_than.go deleted file mode 100644 index 2bee1eb7..00000000 --- a/vercel/validator_string_length_greater_than.go +++ /dev/null @@ -1,42 +0,0 @@ -package vercel - -import ( - "context" - "fmt" - - "github.com/hashicorp/terraform-plugin-framework/schema/validator" -) - -var _ validator.String = validatorStringLengthGreaterThan{} - -func stringLengthGreaterThan(min int) validatorStringLengthGreaterThan { - return validatorStringLengthGreaterThan{ - Min: min, - } -} - -type validatorStringLengthGreaterThan struct { - Min int -} - -func (v validatorStringLengthGreaterThan) Description(ctx context.Context) string { - return fmt.Sprintf("String length must be equal to or greater than %d", v.Min) -} - -func (v validatorStringLengthGreaterThan) MarkdownDescription(ctx context.Context) string { - return fmt.Sprintf("String length must be equal to or greater than %d", v.Min) -} - -func (v validatorStringLengthGreaterThan) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) { - if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() { - return - } - strLen := len(req.ConfigValue.ValueString()) - if strLen < v.Min { - resp.Diagnostics.AddError( - "Invalid value provided", - fmt.Sprintf("String length must be greater than %d, got: %d.", v.Min, strLen), - ) - return - } -} diff --git a/vercel/validator_string_one_of.go b/vercel/validator_string_one_of.go deleted file mode 100644 index 5df30c51..00000000 --- a/vercel/validator_string_one_of.go +++ /dev/null @@ -1,52 +0,0 @@ -package vercel - -import ( - "context" - "fmt" - "strings" - - "github.com/hashicorp/terraform-plugin-framework/schema/validator" -) - -func stringOneOf(items ...string) validatorStringOneOf { - itemMap := map[string]struct{}{} - for _, i := range items { - itemMap[i] = struct{}{} - } - return validatorStringOneOf{ - Items: itemMap, - } -} - -type validatorStringOneOf struct { - Items map[string]struct{} -} - -func (v validatorStringOneOf) keys() (out []string) { - for k := range v.Items { - out = append(out, k) - } - return -} - -func (v validatorStringOneOf) Description(ctx context.Context) string { - return fmt.Sprintf("Item must be one of %s", strings.Join(v.keys(), " ")) -} -func (v validatorStringOneOf) MarkdownDescription(ctx context.Context) string { - return fmt.Sprintf("Item must be one of `%s`", strings.Join(v.keys(), "` `")) -} - -func (v validatorStringOneOf) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) { - if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() { - return - } - - if _, ok := v.Items[req.ConfigValue.ValueString()]; !ok { - resp.Diagnostics.AddAttributeError( - req.Path, - "Invalid value provided", - fmt.Sprintf("Item must be one of %s, got: %s.", strings.Join(v.keys(), ", "), req.ConfigValue.ValueString()), - ) - return - } -} diff --git a/vercel/validator_string_regex.go b/vercel/validator_string_regex.go deleted file mode 100644 index d2f56ba8..00000000 --- a/vercel/validator_string_regex.go +++ /dev/null @@ -1,42 +0,0 @@ -package vercel - -import ( - "context" - "regexp" - - "github.com/hashicorp/terraform-plugin-framework/schema/validator" -) - -func stringRegex(re *regexp.Regexp, errorMessage string) validatorStringRegex { - return validatorStringRegex{ - Re: re, - ErrorMessage: errorMessage, - } -} - -type validatorStringRegex struct { - Re *regexp.Regexp - ErrorMessage string -} - -func (v validatorStringRegex) Description(ctx context.Context) string { - return v.ErrorMessage -} -func (v validatorStringRegex) MarkdownDescription(ctx context.Context) string { - return v.ErrorMessage -} - -func (v validatorStringRegex) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) { - if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() { - return - } - ok := v.Re.MatchString(req.ConfigValue.ValueString()) - if !ok { - resp.Diagnostics.AddAttributeError( - req.Path, - "Invalid value provided", - v.ErrorMessage, - ) - return - } -} diff --git a/vercel/validator_string_set_items_in.go b/vercel/validator_string_set_items_in.go deleted file mode 100644 index d794c6cc..00000000 --- a/vercel/validator_string_set_items_in.go +++ /dev/null @@ -1,65 +0,0 @@ -package vercel - -import ( - "context" - "fmt" - "strings" - - "github.com/hashicorp/terraform-plugin-framework/schema/validator" - "github.com/hashicorp/terraform-plugin-framework/tfsdk" - "github.com/hashicorp/terraform-plugin-framework/types" -) - -func stringSetItemsIn(items ...string) validatorStringSetItemsIn { - itemMap := map[string]struct{}{} - for _, i := range items { - itemMap[i] = struct{}{} - } - return validatorStringSetItemsIn{ - Items: itemMap, - } -} - -type validatorStringSetItemsIn struct { - Items map[string]struct{} -} - -func (v validatorStringSetItemsIn) keys() (out []string) { - for k := range v.Items { - out = append(out, k) - } - return -} - -func (v validatorStringSetItemsIn) Description(ctx context.Context) string { - return fmt.Sprintf("Set item must be one of %s", strings.Join(v.keys(), ", ")) -} -func (v validatorStringSetItemsIn) MarkdownDescription(ctx context.Context) string { - return fmt.Sprintf("Set item must be one of `%s`", strings.Join(v.keys(), ",` `")) -} - -func (v validatorStringSetItemsIn) ValidateSet(ctx context.Context, req validator.SetRequest, resp *validator.SetResponse) { - if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() { - return - } - - for _, i := range req.ConfigValue.Elements() { - var item types.String - if item.IsUnknown() || item.IsNull() { - continue - } - diags := tfsdk.ValueAs(ctx, i, &item) - resp.Diagnostics.Append(diags...) - if diags.HasError() { - return - } - if _, ok := v.Items[item.ValueString()]; !ok { - resp.Diagnostics.AddAttributeError( - req.Path, - "Invalid value provided", - fmt.Sprintf("%s, got %s", v.Description(ctx), item.ValueString()), - ) - return - } - } -} diff --git a/vercel/validator_string_set_min_count.go b/vercel/validator_string_set_min_count.go deleted file mode 100644 index d2eecbaf..00000000 --- a/vercel/validator_string_set_min_count.go +++ /dev/null @@ -1,40 +0,0 @@ -package vercel - -import ( - "context" - "fmt" - - "github.com/hashicorp/terraform-plugin-framework/schema/validator" -) - -func stringSetMinCount(min int) validatorStringSetMinCount { - return validatorStringSetMinCount{ - Min: min, - } -} - -type validatorStringSetMinCount struct { - Min int -} - -func (v validatorStringSetMinCount) Description(ctx context.Context) string { - return fmt.Sprintf("Set must contain %d or more items", v.Min) -} -func (v validatorStringSetMinCount) MarkdownDescription(ctx context.Context) string { - return fmt.Sprintf("Set must contain %d or more items", v.Min) -} - -func (v validatorStringSetMinCount) ValidateSet(ctx context.Context, req validator.SetRequest, resp *validator.SetResponse) { - if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() { - return - } - - if len(req.ConfigValue.Elements()) < v.Min { - resp.Diagnostics.AddAttributeError( - req.Path, - "Invalid value provided", - v.Description(ctx), - ) - return - } -}