Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit22f160d

Browse files
authored
feat: Validate monotonicity for coder_parameter (#90)
* feat: Validate monotonicity for coder_parameter* Fix
1 parent779af7f commit22f160d

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

‎examples/resources/coder_parameter/resource.tf‎

Lines changed: 14 additions & 1 deletion
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‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,19 @@ type Option struct {
2525
}
2626

2727
typeValidationstruct {
28-
Minint
29-
Maxint
28+
Minint
29+
Maxint
30+
Monotonicstring
31+
3032
Regexstring
3133
Errorstring
3234
}
3335

36+
const (
37+
ValidationMonotonicIncreasing="increasing"
38+
ValidationMonotonicDecreasing="decreasing"
39+
)
40+
3441
typeParameterstruct {
3542
Valuestring
3643
Namestring
@@ -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
ifnum>v.Max {
319331
returnfmt.Errorf("value %d is more than the maximum %d",num,v.Max)
320332
}
333+
ifv.Monotonic!=""&&v.Monotonic!=ValidationMonotonicIncreasing&&v.Monotonic!=ValidationMonotonicDecreasing {
334+
returnfmt.Errorf("number monotonicity can be either %q or %q",ValidationMonotonicIncreasing,ValidationMonotonicDecreasing)
335+
}
321336
}
322337
returnnil
323338
}

‎provider/parameter_test.go‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ func TestValueValidatesType(t *testing.T) {
271271
RegexErrorstring
272272
Min,
273273
Maxint
274-
Error*regexp.Regexp
274+
Monotonicstring
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
iftc.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)

[8]ページ先頭

©2009-2025 Movatter.jp