这是indexloc提供的服务,不要输入任何密码
Skip to content

Use a validator library instead of custom validation where possible #230

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

Merged
merged 1 commit into from
Oct 29, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions vercel/data_source_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
),
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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"),
},
},
},
Expand Down
8 changes: 6 additions & 2 deletions vercel/data_source_shared_environment_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
3 changes: 2 additions & 1 deletion vercel/resource_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
26 changes: 14 additions & 12 deletions vercel/resource_dns_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand Down
3 changes: 2 additions & 1 deletion vercel/resource_edge_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.",
),
Expand Down
3 changes: 2 additions & 1 deletion vercel/resource_edge_config_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
22 changes: 13 additions & 9 deletions vercel/resource_log_drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand Down
Loading
Loading