Skip to content

Commit 6fbb918

Browse files
committed
WIP
1 parent dbc530b commit 6fbb918

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

provider/parameter.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ type Option struct {
2626
}
2727

2828
type Validation struct {
29-
Min int
30-
Max int
29+
Min *int
30+
Max *int
3131
Monotonic string
3232

3333
Regex string
@@ -272,17 +272,14 @@ func parameterDataSource() *schema.Resource {
272272
Elem: &schema.Resource{
273273
Schema: map[string]*schema.Schema{
274274
"min": {
275-
Type: schema.TypeInt,
276-
Optional: true,
277-
Default: 0,
278-
Description: "The minimum of a number parameter.",
279-
RequiredWith: []string{"validation.0.max"},
275+
Type: schema.TypeInt,
276+
Optional: true,
277+
Description: "The minimum of a number parameter.",
280278
},
281279
"max": {
282-
Type: schema.TypeInt,
283-
Optional: true,
284-
Description: "The maximum of a number parameter.",
285-
RequiredWith: []string{"validation.0.min"},
280+
Type: schema.TypeInt,
281+
Optional: true,
282+
Description: "The maximum of a number parameter.",
286283
},
287284
"monotonic": {
288285
Type: schema.TypeString,
@@ -353,10 +350,10 @@ func valueIsType(typ, value string) diag.Diagnostics {
353350

354351
func (v *Validation) Valid(typ, value string) error {
355352
if typ != "number" {
356-
if v.Min != 0 {
353+
if v.Min != nil {
357354
return fmt.Errorf("a min cannot be specified for a %s type", typ)
358355
}
359-
if v.Max != 0 {
356+
if v.Max != nil {
360357
return fmt.Errorf("a max cannot be specified for a %s type", typ)
361358
}
362359
}
@@ -389,10 +386,10 @@ func (v *Validation) Valid(typ, value string) error {
389386
if err != nil {
390387
return fmt.Errorf("value %q is not a number", value)
391388
}
392-
if num < v.Min {
389+
if v.Min != nil && num < *v.Min {
393390
return fmt.Errorf("value %d is less than the minimum %d", num, v.Min)
394391
}
395-
if num > v.Max {
392+
if v.Max != nil && num > *v.Max {
396393
return fmt.Errorf("value %d is more than the maximum %d", num, v.Max)
397394
}
398395
if v.Monotonic != "" && v.Monotonic != ValidationMonotonicIncreasing && v.Monotonic != ValidationMonotonicDecreasing {

provider/parameter_test.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -443,18 +443,18 @@ func TestValueValidatesType(t *testing.T) {
443443
Regex,
444444
RegexError string
445445
Min,
446-
Max int
446+
Max *int
447447
Monotonic string
448448
Error *regexp.Regexp
449449
}{{
450450
Name: "StringWithMin",
451451
Type: "string",
452-
Min: 1,
452+
Min: ptrNumber(1),
453453
Error: regexp.MustCompile("cannot be specified"),
454454
}, {
455455
Name: "StringWithMax",
456456
Type: "string",
457-
Max: 1,
457+
Max: ptrNumber(1),
458458
Error: regexp.MustCompile("cannot be specified"),
459459
}, {
460460
Name: "NonStringWithRegex",
@@ -474,13 +474,13 @@ func TestValueValidatesType(t *testing.T) {
474474
Name: "NumberBelowMin",
475475
Type: "number",
476476
Value: "0",
477-
Min: 1,
477+
Min: ptrNumber(1),
478478
Error: regexp.MustCompile("is less than the minimum"),
479479
}, {
480480
Name: "NumberAboveMax",
481481
Type: "number",
482482
Value: "1",
483-
Max: 0,
483+
Max: ptrNumber(1),
484484
Error: regexp.MustCompile("is more than the maximum"),
485485
}, {
486486
Name: "InvalidBool",
@@ -498,23 +498,23 @@ func TestValueValidatesType(t *testing.T) {
498498
Name: "InvalidMonotonicity",
499499
Type: "number",
500500
Value: "1",
501-
Min: 0,
502-
Max: 2,
501+
Min: ptrNumber(0),
502+
Max: ptrNumber(2),
503503
Monotonic: "foobar",
504504
Error: regexp.MustCompile(`number monotonicity can be either "increasing" or "decreasing"`),
505505
}, {
506506
Name: "IncreasingMonotonicity",
507507
Type: "number",
508508
Value: "1",
509-
Min: 0,
510-
Max: 2,
509+
Min: ptrNumber(0),
510+
Max: ptrNumber(2),
511511
Monotonic: "increasing",
512512
}, {
513513
Name: "DecreasingMonotonicity",
514514
Type: "number",
515515
Value: "1",
516-
Min: 0,
517-
Max: 2,
516+
Min: ptrNumber(0),
517+
Max: ptrNumber(2),
518518
Monotonic: "decreasing",
519519
}, {
520520
Name: "ValidListOfStrings",
@@ -550,3 +550,7 @@ func TestValueValidatesType(t *testing.T) {
550550
})
551551
}
552552
}
553+
554+
func ptrNumber(i int) *int {
555+
return &i
556+
}

0 commit comments

Comments
 (0)