Skip to content

Commit

Permalink
Add validation to type names in product_type / type
Browse files Browse the repository at this point in the history
Validate it in terraform instead of relying on commercetools errors.
Note that we should do this on way more places
  • Loading branch information
mvantellingen committed Jul 8, 2022
1 parent f2436a7 commit 8e0f25c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
24 changes: 24 additions & 0 deletions commercetools/resource_product_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/elliotchance/orderedmap/v2"
"github.com/elliotchance/pie/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -158,6 +159,29 @@ func attributeTypeElement(setsAllowed bool) *schema.Resource {
Required: true,
ValidateFunc: func(val any, key string) (warns []string, errs []error) {
v := val.(string)
validValues := []string{
"boolean",
"text",
"ltext",
"enum",
"lenum",
"number",
"money",
"date",
"time",
"datetime",
"reference",
"set",
"nested",
}

if !pie.Contains(validValues, v) {
errs = append(errs, fmt.Errorf("%s is not a valid type. Valid types are: %s",
v, strings.Join(pie.SortStableUsing(validValues, func(a, b string) bool {
return a < b
}), ", ")))
}

if !setsAllowed && v == "set" {
errs = append(errs, fmt.Errorf("sets in another Set are not allowed"))
}
Expand Down
23 changes: 23 additions & 0 deletions commercetools/resource_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/elliotchance/orderedmap/v2"
"github.com/elliotchance/pie/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -350,6 +351,28 @@ func fieldTypeElement(setsAllowed bool) *schema.Resource {
Required: true,
ValidateFunc: func(val any, key string) (warns []string, errs []error) {
v := val.(string)
validValues := []string{
"Boolean",
"Number",
"String",
"LocalizedString",
"Enum",
"LocalizedEnum",
"Money",
"Date",
"Time",
"DateTime",
"Reference",
"Set",
}

if !pie.Contains(validValues, v) {
errs = append(errs, fmt.Errorf("%s is not a valid type. Valid types are: %s",
v, strings.Join(pie.SortStableUsing(validValues, func(a, b string) bool {
return a < b
}), ", ")))
}

if !setsAllowed && v == "Set" {
errs = append(errs, fmt.Errorf("sets in another Set are not allowed"))
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/armon/go-radix v1.0.0 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/elliotchance/pie/v2 v2.0.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elliotchance/orderedmap/v2 v2.0.1 h1:CWEyejE1516ugF5TScffjSSUzKd1czkTOBxr/vlkCLU=
github.com/elliotchance/orderedmap/v2 v2.0.1/go.mod h1:85lZyVbpGaGvHvnKa7Qhx7zncAdBIBq6u56Hb1PRU5Q=
github.com/elliotchance/pie/v2 v2.0.1 h1:TW6imTMwc8h51AaWNLxjyx0eNUtzAaB6ZCjKzTahYHw=
github.com/elliotchance/pie/v2 v2.0.1/go.mod h1:18t0dgGFH006g4eVdDtWfgFZPQEgl10IoEO8YWEq3Og=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down

0 comments on commit 8e0f25c

Please sign in to comment.