Skip to content

Commit 18c3d05

Browse files
committed
add a test case for invalid multi selecT
1 parent f0bca91 commit 18c3d05

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

provider/formtype.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ func ValidateFormType(paramType OptionType, optionCount int, specifiedFormType P
127127
return paramType, specifiedFormType, xerrors.Errorf("value type %q is not supported for 'form_types'", specifiedFormType)
128128
}
129129

130-
// Special case
130+
// This is the only current special case. If 'multi-select' is selected, the type
131+
// of 'value' and an options 'value' are different. The type of the parameter is
132+
// `list(string)` but the type of the individual options is `string`.
131133
if paramType == OptionTypeListString && specifiedFormType == ParameterFormTypeMultiSelect {
132134
return OptionTypeString, ParameterFormTypeMultiSelect, nil
133135
}

provider/formtype_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,19 @@ func TestValidateFormType(t *testing.T) {
220220
optionType: provider.OptionTypeListString,
221221
formType: provider.ParameterFormTypeTagSelect,
222222
}),
223+
224+
// Some manual test cases
225+
{
226+
name: "list_string_bad_default",
227+
config: formTypeCheck{
228+
formType: provider.ParameterFormTypeMultiSelect,
229+
optionType: provider.OptionTypeListString,
230+
customOptions: []string{"red", "blue", "green"},
231+
defValue: `["red", "yellow"]`,
232+
styling: nil,
233+
},
234+
expectError: regexp.MustCompile("is not a valid option"),
235+
},
223236
}
224237

225238
t.Run("TabledTests", func(t *testing.T) {

provider/parameter.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,17 @@ func parameterDataSource() *schema.Resource {
153153
}
154154

155155
// Validate options
156+
157+
// optionType might differ from parameter.Type. This is ok, and parameter.Type
158+
// should be used for the value type, and optionType for options.
156159
var optionType OptionType
157160
optionType, parameter.FormType, err = ValidateFormType(parameter.Type, len(parameter.Option), parameter.FormType)
158161
if err != nil {
159162
return diag.FromErr(err)
160163
}
161-
// Set the form_type back in case the value was changed, eg via a
162-
// default.
164+
// Set the form_type back in case the value was changed.
165+
// Eg via a default. If a user does not specify, a default value
166+
// is used and saved.
163167
rd.Set("form_type", parameter.FormType)
164168

165169
if len(parameter.Option) > 0 {
@@ -187,11 +191,13 @@ func parameterDataSource() *schema.Resource {
187191
// If the type is list(string) and optionType is string, we have
188192
// to ensure all elements of the default exist as options.
189193
var defaultValues []string
194+
// TODO: We do this unmarshal in a few spots. It should be standardized.
190195
err = json.Unmarshal([]byte(parameter.Default), &defaultValues)
191196
if err != nil {
192197
return diag.Errorf("default value %q is not a list of strings", parameter.Default)
193198
}
194199

200+
// missing is used to construct a more helpful error message
195201
var missing []string
196202
for _, defaultValue := range defaultValues {
197203
_, defaultIsValid := values[defaultValue]
@@ -206,7 +212,6 @@ func parameterDataSource() *schema.Resource {
206212
parameter.Default, strings.Join(missing, ", "),
207213
)
208214
}
209-
210215
} else {
211216
_, defaultIsValid := values[parameter.Default]
212217
if !defaultIsValid {

0 commit comments

Comments
 (0)