Skip to content

Commit 22f160d

Browse files
authored
feat: Validate monotonicity for coder_parameter (#90)
* feat: Validate monotonicity for coder_parameter * Fix
1 parent 779af7f commit 22f160d

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

examples/resources/coder_parameter/resource.tf

+14-1
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,23 @@ data "coder_parameter" "cores" {
4040
data "coder_parameter" "disk_size" {
4141
name = "Disk Size"
4242
type = "number"
43+
default = "5"
44+
validation {
45+
# This can apply to number.
46+
min = 0
47+
max = 10
48+
monotonic = "increasing"
49+
}
50+
}
51+
52+
data "coder_parameter" "cat_lives" {
53+
name = "Cat Live"
54+
type = "number"
4355
default = "9"
4456
validation {
45-
# This can apply to number and string types.
57+
# This can apply to number.
4658
min = 0
4759
max = 10
60+
monotonic = "decreasing"
4861
}
4962
}

provider/parameter.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,19 @@ type Option struct {
2525
}
2626

2727
type Validation struct {
28-
Min int
29-
Max int
28+
Min int
29+
Max int
30+
Monotonic string
31+
3032
Regex string
3133
Error string
3234
}
3335

36+
const (
37+
ValidationMonotonicIncreasing = "increasing"
38+
ValidationMonotonicDecreasing = "decreasing"
39+
)
40+
3441
type Parameter struct {
3542
Value string
3643
Name string
@@ -235,9 +242,14 @@ func parameterDataSource() *schema.Resource {
235242
Description: "The maximum of a number parameter.",
236243
RequiredWith: []string{"validation.0.min"},
237244
},
245+
"monotonic": {
246+
Type: schema.TypeString,
247+
Optional: true,
248+
Description: "Number monotonicity, either increasing or decreasing.",
249+
},
238250
"regex": {
239251
Type: schema.TypeString,
240-
ConflictsWith: []string{"validation.0.min", "validation.0.max"},
252+
ConflictsWith: []string{"validation.0.min", "validation.0.max", "validation.0.monotonic"},
241253
Description: "A regex for the input parameter to match against.",
242254
Optional: true,
243255
},
@@ -318,6 +330,9 @@ func (v *Validation) Valid(typ, value string) error {
318330
if num > v.Max {
319331
return fmt.Errorf("value %d is more than the maximum %d", num, v.Max)
320332
}
333+
if v.Monotonic != "" && v.Monotonic != ValidationMonotonicIncreasing && v.Monotonic != ValidationMonotonicDecreasing {
334+
return fmt.Errorf("number monotonicity can be either %q or %q", ValidationMonotonicIncreasing, ValidationMonotonicDecreasing)
335+
}
321336
}
322337
return nil
323338
}

provider/parameter_test.go

+30-5
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ func TestValueValidatesType(t *testing.T) {
271271
RegexError string
272272
Min,
273273
Max int
274-
Error *regexp.Regexp
274+
Monotonic string
275+
Error *regexp.Regexp
275276
}{{
276277
Name: "StringWithMin",
277278
Type: "string",
@@ -320,18 +321,42 @@ func TestValueValidatesType(t *testing.T) {
320321
RegexError: "bad fruit",
321322
Value: "apple",
322323
Error: regexp.MustCompile(`bad fruit`),
324+
}, {
325+
Name: "InvalidMonotonicity",
326+
Type: "number",
327+
Value: "1",
328+
Min: 0,
329+
Max: 2,
330+
Monotonic: "foobar",
331+
Error: regexp.MustCompile(`number monotonicity can be either "increasing" or "decreasing"`),
332+
}, {
333+
Name: "IncreasingMonotonicity",
334+
Type: "number",
335+
Value: "1",
336+
Min: 0,
337+
Max: 2,
338+
Monotonic: "increasing",
339+
}, {
340+
Name: "DecreasingMonotonicity",
341+
Type: "number",
342+
Value: "1",
343+
Min: 0,
344+
Max: 2,
345+
Monotonic: "decreasing",
323346
}} {
324347
tc := tc
325348
t.Run(tc.Name, func(t *testing.T) {
326349
t.Parallel()
327350
v := &provider.Validation{
328-
Min: tc.Min,
329-
Max: tc.Max,
330-
Regex: tc.Regex,
331-
Error: tc.RegexError,
351+
Min: tc.Min,
352+
Max: tc.Max,
353+
Monotonic: tc.Monotonic,
354+
Regex: tc.Regex,
355+
Error: tc.RegexError,
332356
}
333357
err := v.Valid(tc.Type, tc.Value)
334358
if tc.Error != nil {
359+
require.Error(t, err)
335360
require.True(t, tc.Error.MatchString(err.Error()), "got: %s", err.Error())
336361
}
337362
})

0 commit comments

Comments
 (0)