Skip to content

Commit 8e0f25c

Browse files
committed
Add validation to type names in product_type / type
Validate it in terraform instead of relying on commercetools errors. Note that we should do this on way more places
1 parent f2436a7 commit 8e0f25c

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

commercetools/resource_product_type.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/elliotchance/orderedmap/v2"
11+
"github.com/elliotchance/pie/v2"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -158,6 +159,29 @@ func attributeTypeElement(setsAllowed bool) *schema.Resource {
158159
Required: true,
159160
ValidateFunc: func(val any, key string) (warns []string, errs []error) {
160161
v := val.(string)
162+
validValues := []string{
163+
"boolean",
164+
"text",
165+
"ltext",
166+
"enum",
167+
"lenum",
168+
"number",
169+
"money",
170+
"date",
171+
"time",
172+
"datetime",
173+
"reference",
174+
"set",
175+
"nested",
176+
}
177+
178+
if !pie.Contains(validValues, v) {
179+
errs = append(errs, fmt.Errorf("%s is not a valid type. Valid types are: %s",
180+
v, strings.Join(pie.SortStableUsing(validValues, func(a, b string) bool {
181+
return a < b
182+
}), ", ")))
183+
}
184+
161185
if !setsAllowed && v == "set" {
162186
errs = append(errs, fmt.Errorf("sets in another Set are not allowed"))
163187
}

commercetools/resource_type.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/elliotchance/orderedmap/v2"
11+
"github.com/elliotchance/pie/v2"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -350,6 +351,28 @@ func fieldTypeElement(setsAllowed bool) *schema.Resource {
350351
Required: true,
351352
ValidateFunc: func(val any, key string) (warns []string, errs []error) {
352353
v := val.(string)
354+
validValues := []string{
355+
"Boolean",
356+
"Number",
357+
"String",
358+
"LocalizedString",
359+
"Enum",
360+
"LocalizedEnum",
361+
"Money",
362+
"Date",
363+
"Time",
364+
"DateTime",
365+
"Reference",
366+
"Set",
367+
}
368+
369+
if !pie.Contains(validValues, v) {
370+
errs = append(errs, fmt.Errorf("%s is not a valid type. Valid types are: %s",
371+
v, strings.Join(pie.SortStableUsing(validValues, func(a, b string) bool {
372+
return a < b
373+
}), ", ")))
374+
}
375+
353376
if !setsAllowed && v == "Set" {
354377
errs = append(errs, fmt.Errorf("sets in another Set are not allowed"))
355378
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
github.com/armon/go-radix v1.0.0 // indirect
2020
github.com/bgentry/speakeasy v0.1.0 // indirect
2121
github.com/davecgh/go-spew v1.1.1 // indirect
22+
github.com/elliotchance/pie/v2 v2.0.1 // indirect
2223
github.com/fatih/color v1.13.0 // indirect
2324
github.com/golang/protobuf v1.5.2 // indirect
2425
github.com/google/go-cmp v0.5.8 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
8787
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8888
github.com/elliotchance/orderedmap/v2 v2.0.1 h1:CWEyejE1516ugF5TScffjSSUzKd1czkTOBxr/vlkCLU=
8989
github.com/elliotchance/orderedmap/v2 v2.0.1/go.mod h1:85lZyVbpGaGvHvnKa7Qhx7zncAdBIBq6u56Hb1PRU5Q=
90+
github.com/elliotchance/pie/v2 v2.0.1 h1:TW6imTMwc8h51AaWNLxjyx0eNUtzAaB6ZCjKzTahYHw=
91+
github.com/elliotchance/pie/v2 v2.0.1/go.mod h1:18t0dgGFH006g4eVdDtWfgFZPQEgl10IoEO8YWEq3Og=
9092
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
9193
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
9294
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=

0 commit comments

Comments
 (0)