Skip to content

Commit fbd613e

Browse files
committed
remove duped validation
1 parent dd7af3b commit fbd613e

File tree

2 files changed

+14
-28
lines changed

2 files changed

+14
-28
lines changed

provider/parameter.go

+10-24
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,16 @@ func valueIsType(typ OptionType, value string) error {
410410
}
411411

412412
func (v *Parameter) Valid(input *string, mode ValidationMode) (string, diag.Diagnostics) {
413+
if mode != ValidationModeDefault && mode != ValidationModeTemplateImport {
414+
return "", diag.Diagnostics{
415+
{
416+
Severity: diag.Error,
417+
Summary: "Invalid validation mode",
418+
Detail: fmt.Sprintf("validation mode %q is not supported, use %q, or %q", mode, ValidationModeDefault, ValidationModeTemplateImport),
419+
},
420+
}
421+
}
422+
413423
var err error
414424
var optionType OptionType
415425

@@ -465,30 +475,6 @@ func (v *Parameter) Valid(input *string, mode ValidationMode) (string, diag.Diag
465475
}
466476
}
467477

468-
// TODO: Move this into 'Parameter.validValue'. It exists as another check outside because
469-
// the current behavior is to always apply this validation, regardless if the param is set or not.
470-
// Other validations are only applied if the parameter is set.
471-
// This behavior should be standardized.
472-
if len(v.Validation) == 1 {
473-
empty := ""
474-
validVal := value
475-
if value == nil {
476-
validVal = &empty
477-
}
478-
validCheck := &v.Validation[0]
479-
err := validCheck.Valid(v.Type, *validVal)
480-
if err != nil {
481-
return "", diag.Diagnostics{
482-
{
483-
Severity: diag.Error,
484-
Summary: fmt.Sprintf("Invalid parameter %s according to 'validation' block", strings.ToLower(v.Name)),
485-
Detail: err.Error(),
486-
AttributePath: cty.Path{},
487-
},
488-
}
489-
}
490-
}
491-
492478
// TODO: This is a bit of a hack. The current behavior states if validation
493479
// is given, then apply validation to unset values.
494480
// This should be removed, and all values should be validated. Meaning

provider/testdata/parameter_table.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
| EmptyOpts | string,number | | | 1,2,3 | | | "" | false | | |
1010
| EmptyRegex | string | | | | world | | | | regex error | = |
1111
| EmptyMin | number | | | | 1-10 | | | | 1 < < 10 | = |
12-
| EmptyMinOpt | number | | | 1,2,3 | 2-5 | | | | 2 < < 5 | 2 < 1 < 5 |
13-
| EmptyRegexOpt | string | | | "hello","goodbye" | goodbye | | | | regex error | = |
12+
| EmptyMinOpt | number | | | 1,2,3 | 2-5 | | | | valid option | 2 < 1 < 5 |
13+
| EmptyRegexOpt | string | | | "hello","goodbye" | goodbye | | | | valid option | regex error |
1414
| EmptyRegexOk | string | | | | .* | | "" | false | | |
1515
| | | | | | | | | | | |
1616
| | Default Set | No inputs | | | | | | | | |
@@ -48,7 +48,7 @@
4848
| NumIns=DefInv | number | 5 | 5 | | 1-3 | | | | 1 < 5 < 3 | = |
4949
| NumInsOpts | number | 3 | 5 | 1,2,3,4,5 | 1-3 | | 3 | true | | 1 < 5 < 3 |
5050
| NumInsNotOptsVal | number | 3 | 5 | 1,2,4,5 | 1-3 | | | | valid option | 1 < 4 < 3 |
51-
| NumInsNotOptsInv | number | 3 | 5 | 1,2,4,5 | 1-2 | | | true | 1 < 3 < 2 | 1 < 4 < 2 |
51+
| NumInsNotOptsInv | number | 3 | 5 | 1,2,4,5 | 1-2 | | | true | valid option | 1 < 4 < 2 |
5252
| NumInsNotOpts | number | 3 | 5 | 1,2,4,5 | | | | | valid option | = |
5353
| NumInsNotOpts/NoDef | number | 3 | | 1,2,4,5 | | | | | valid option | = |
5454
| | | | | | | | | | | |
@@ -59,7 +59,7 @@
5959
| StrIns=DefInv | string | e | e | | [a-c] | | | | regex error | = |
6060
| StrInsOpts | string | c | e | a,b,c,d,e | [a-c] | | c | true | | value "d" |
6161
| StrInsNotOptsVal | string | c | e | a,b,d,e | [a-c] | | | | valid option | value "d" |
62-
| StrInsNotOptsInv | string | c | e | a,b,d,e | [a-b] | | | | regex error | = |
62+
| StrInsNotOptsInv | string | c | e | a,b,d,e | [a-b] | | | | valid option | regex error |
6363
| StrInsNotOpts | string | c | e | a,b,d,e | | | | | valid option | = |
6464
| StrInsNotOpts/NoDef | string | c | | a,b,d,e | | | | | valid option | = |
6565
| StrInsBadVal | string | c | | a,b,c,d,e | 1-10 | | | | min cannot | = |

0 commit comments

Comments
 (0)