Skip to content

Commit df13514

Browse files
author
Ivan De Marino
committed
PR review
1 parent 699e751 commit df13514

21 files changed

+40
-26
lines changed

.changelog/42.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ numbervalidator: New package that starts with 2 validation functions, `OneOf()`
1111
```
1212

1313
```release-note:enhancement
14-
stringvalidator: 2 new validation functions, `OneOf()` and `NoneOf()`, similar to the ones in `genericvalidator` but option to control case-sensitivity
14+
stringvalidator: 2 new validation functions, `OneOf()` and `NoneOf()`, that offer case-sensitivity control
1515
```

float64validator/at_least.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (validator atLeastValidator) Validate(ctx context.Context, request tfsdk.Va
3434
}
3535

3636
if f < validator.min {
37-
response.Diagnostics.Append(validatordiag.InvalidValueDiagnostic(
37+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
3838
request.AttributePath,
3939
validator.Description(ctx),
4040
fmt.Sprintf("%f", f),

float64validator/at_most.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (validator atMostValidator) Validate(ctx context.Context, request tfsdk.Val
3434
}
3535

3636
if f > validator.max {
37-
response.Diagnostics.Append(validatordiag.InvalidValueDiagnostic(
37+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
3838
request.AttributePath,
3939
validator.Description(ctx),
4040
fmt.Sprintf("%f", f),

float64validator/between.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (validator betweenValidator) Validate(ctx context.Context, request tfsdk.Va
3434
}
3535

3636
if f < validator.min || f > validator.max {
37-
response.Diagnostics.Append(validatordiag.InvalidValueDiagnostic(
37+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
3838
request.AttributePath,
3939
validator.Description(ctx),
4040
fmt.Sprintf("%f", f),

float64validator/type_validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func validateFloat(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (float64, bool) {
1313
t := request.AttributeConfig.Type(ctx)
1414
if t != types.Float64Type {
15-
response.Diagnostics.Append(validatordiag.InvalidTypeDiagnostic(
15+
response.Diagnostics.Append(validatordiag.InvalidAttributeTypeDiagnostic(
1616
request.AttributePath,
1717
"Expected value of type float64",
1818
t.String(),

helpers/validatordiag/diag.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,44 @@ import (
88
"github.com/hashicorp/terraform-plugin-go/tftypes"
99
)
1010

11-
// InvalidValueDiagnostic returns an error Diagnostic to be used when an attribute has an invalid value.
12-
func InvalidValueDiagnostic(path *tftypes.AttributePath, description string, value string) diag.Diagnostic {
11+
// InvalidAttributeValueDiagnostic returns an error Diagnostic to be used when an attribute has an invalid value.
12+
func InvalidAttributeValueDiagnostic(path *tftypes.AttributePath, description string, value string) diag.Diagnostic {
1313
return diag.NewAttributeErrorDiagnostic(
1414
path,
1515
"Invalid Attribute Value",
1616
capitalize(description)+", got: "+value,
1717
)
1818
}
1919

20-
// InvalidValueLengthDiagnostic returns an error Diagnostic to be used when an attribute's value has an invalid length.
21-
func InvalidValueLengthDiagnostic(path *tftypes.AttributePath, description string, value string) diag.Diagnostic {
20+
// InvalidAttributeValueLengthDiagnostic returns an error Diagnostic to be used when an attribute's value has an invalid length.
21+
func InvalidAttributeValueLengthDiagnostic(path *tftypes.AttributePath, description string, value string) diag.Diagnostic {
2222
return diag.NewAttributeErrorDiagnostic(
2323
path,
2424
"Invalid Attribute Value Length",
2525
capitalize(description)+", got: "+value,
2626
)
2727
}
2828

29-
// InvalidValueMatchDiagnostic returns an error Diagnostic to be used when an attribute's value has an invalid match.
30-
func InvalidValueMatchDiagnostic(path *tftypes.AttributePath, description string, value string) diag.Diagnostic {
29+
// InvalidAttributeValueMatchDiagnostic returns an error Diagnostic to be used when an attribute's value has an invalid match.
30+
func InvalidAttributeValueMatchDiagnostic(path *tftypes.AttributePath, description string, value string) diag.Diagnostic {
3131
return diag.NewAttributeErrorDiagnostic(
3232
path,
3333
"Invalid Attribute Value Match",
3434
capitalize(description)+", got: "+value,
3535
)
3636
}
3737

38-
// InvalidSchemaDiagnostic returns an error Diagnostic to be used when a schemavalidator of attributes is invalid.
39-
func InvalidSchemaDiagnostic(path *tftypes.AttributePath, description string) diag.Diagnostic {
38+
// InvalidAttributeSchemaDiagnostic returns an error Diagnostic to be used when a schemavalidator of attributes is invalid.
39+
func InvalidAttributeSchemaDiagnostic(path *tftypes.AttributePath, description string) diag.Diagnostic {
4040
return diag.NewAttributeErrorDiagnostic(
4141
path,
4242
"Invalid Attribute Combination",
4343
capitalize(description),
4444
)
4545
}
4646

47-
// InvalidTypeDiagnostic returns an error Diagnostic to be used when an attribute has an invalid type.
48-
func InvalidTypeDiagnostic(path *tftypes.AttributePath, description string, value string) diag.Diagnostic {
47+
// InvalidAttributeTypeDiagnostic returns an error Diagnostic to be used when an attribute has an invalid type.
48+
func InvalidAttributeTypeDiagnostic(path *tftypes.AttributePath, description string, value string) diag.Diagnostic {
4949
return diag.NewAttributeErrorDiagnostic(
5050
path,
5151
"Invalid Attribute Type",

int64validator/at_least.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (validator atLeastValidator) Validate(ctx context.Context, request tfsdk.Va
3434
}
3535

3636
if i < validator.min {
37-
response.Diagnostics.Append(validatordiag.InvalidValueDiagnostic(
37+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
3838
request.AttributePath,
3939
validator.Description(ctx),
4040
fmt.Sprintf("%d", i),

int64validator/at_most.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (validator atMostValidator) Validate(ctx context.Context, request tfsdk.Val
3434
}
3535

3636
if i > validator.max {
37-
response.Diagnostics.Append(validatordiag.InvalidValueDiagnostic(
37+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
3838
request.AttributePath,
3939
validator.Description(ctx),
4040
fmt.Sprintf("%d", i),

int64validator/between.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (validator betweenValidator) Validate(ctx context.Context, request tfsdk.Va
3434
}
3535

3636
if i < validator.min || i > validator.max {
37-
response.Diagnostics.Append(validatordiag.InvalidValueDiagnostic(
37+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
3838
request.AttributePath,
3939
validator.Description(ctx),
4040
fmt.Sprintf("%d", i),

int64validator/type_validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func validateInt(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (int64, bool) {
1313
t := request.AttributeConfig.Type(ctx)
1414
if t != types.Int64Type {
15-
response.Diagnostics.Append(validatordiag.InvalidTypeDiagnostic(
15+
response.Diagnostics.Append(validatordiag.InvalidAttributeTypeDiagnostic(
1616
request.AttributePath,
1717
"Expected value of type int64",
1818
t.String(),

internal/primitivevalidator/acceptable_values_validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (av *acceptablePrimitiveValuesAttributeValidator) Validate(ctx context.Cont
5151

5252
if av.shouldMatch && !av.isAcceptableValue(value) || //< EITHER should match but it does not
5353
!av.shouldMatch && av.isAcceptableValue(value) { //< OR should not match but it does
54-
res.Diagnostics.Append(validatordiag.InvalidValueMatchDiagnostic(
54+
res.Diagnostics.Append(validatordiag.InvalidAttributeValueMatchDiagnostic(
5555
req.AttributePath,
5656
av.Description(ctx),
5757
value.String(),

internal/primitivevalidator/none_of_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func TestNoneOfValidator(t *testing.T) {
5050
types.String{Value: "bar"},
5151
types.String{Value: "baz"},
5252
),
53+
expErrors: 0,
5354
},
5455
"mixed": {
5556
in: types.Float64{Value: 1.234},
@@ -154,6 +155,7 @@ func TestNoneOfValidator(t *testing.T) {
154155
types.String{Value: "bar"},
155156
types.String{Value: "baz"},
156157
),
158+
expErrors: 0,
157159
},
158160
"skip-validation-on-unknown": {
159161
in: types.String{Unknown: true},
@@ -162,6 +164,7 @@ func TestNoneOfValidator(t *testing.T) {
162164
types.String{Value: "bar"},
163165
types.String{Value: "baz"},
164166
),
167+
expErrors: 0,
165168
},
166169
}
167170

internal/primitivevalidator/one_of_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func TestOneOfValidator(t *testing.T) {
4141
types.String{Value: "bar"},
4242
types.String{Value: "baz"},
4343
),
44+
expErrors: 0,
4445
},
4546
"simple-mismatch": {
4647
in: types.String{Value: "foz"},
@@ -58,6 +59,7 @@ func TestOneOfValidator(t *testing.T) {
5859
types.Int64{Value: 567},
5960
types.Float64{Value: 1.234},
6061
),
62+
expErrors: 0,
6163
},
6264
"list-not-allowed": {
6365
in: types.List{
@@ -153,6 +155,7 @@ func TestOneOfValidator(t *testing.T) {
153155
types.String{Value: "bar"},
154156
types.String{Value: "baz"},
155157
),
158+
expErrors: 0,
156159
},
157160
"skip-validation-on-unknown": {
158161
in: types.String{Unknown: true},
@@ -161,6 +164,7 @@ func TestOneOfValidator(t *testing.T) {
161164
types.String{Value: "bar"},
162165
types.String{Value: "baz"},
163166
),
167+
expErrors: 0,
164168
},
165169
}
166170

stringvalidator/acceptable_strings_validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (av *acceptableStringsAttributeValidator) Validate(ctx context.Context, req
3838

3939
if av.shouldMatch && !av.isAcceptableValue(value) || //< EITHER should match but it does not
4040
!av.shouldMatch && av.isAcceptableValue(value) { //< OR should not match but it does
41-
res.Diagnostics.Append(validatordiag.InvalidValueMatchDiagnostic(
41+
res.Diagnostics.Append(validatordiag.InvalidAttributeValueMatchDiagnostic(
4242
req.AttributePath,
4343
av.Description(ctx),
4444
value,

stringvalidator/length_at_least.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (validator lengthAtLeastValidator) Validate(ctx context.Context, request tf
3434
}
3535

3636
if l := len(s); l < validator.minLength {
37-
response.Diagnostics.Append(validatordiag.InvalidValueLengthDiagnostic(
37+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueLengthDiagnostic(
3838
request.AttributePath,
3939
validator.Description(ctx),
4040
fmt.Sprintf("%d", l),

stringvalidator/length_at_most.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (validator lengthAtMostValidator) Validate(ctx context.Context, request tfs
3434
}
3535

3636
if l := len(s); l > validator.maxLength {
37-
response.Diagnostics.Append(validatordiag.InvalidValueLengthDiagnostic(
37+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueLengthDiagnostic(
3838
request.AttributePath,
3939
validator.Description(ctx),
4040
fmt.Sprintf("%d", l),

stringvalidator/length_between.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (validator lengthBetweenValidator) Validate(ctx context.Context, request tf
3434
}
3535

3636
if l := len(s); l < validator.minLength || l > validator.maxLength {
37-
response.Diagnostics.Append(validatordiag.InvalidValueLengthDiagnostic(
37+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueLengthDiagnostic(
3838
request.AttributePath,
3939
validator.Description(ctx),
4040
fmt.Sprintf("%d", l),

stringvalidator/none_of_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func TestNoneOfValidator(t *testing.T) {
5555
"bar",
5656
"baz",
5757
),
58+
expErrors: 0,
5859
},
5960
"list-not-allowed": {
6061
in: types.List{
@@ -140,6 +141,7 @@ func TestNoneOfValidator(t *testing.T) {
140141
"bar",
141142
"baz",
142143
),
144+
expErrors: 0,
143145
},
144146
"skip-validation-on-unknown": {
145147
in: types.String{Unknown: true},
@@ -149,6 +151,7 @@ func TestNoneOfValidator(t *testing.T) {
149151
"bar",
150152
"baz",
151153
),
154+
expErrors: 0,
152155
},
153156
}
154157

stringvalidator/one_of_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func TestOneOfValidator(t *testing.T) {
3535
"bar",
3636
"baz",
3737
),
38+
expErrors: 0,
3839
},
3940
"simple-match-case-insensitive": {
4041
in: types.String{Value: "foo"},
@@ -44,6 +45,7 @@ func TestOneOfValidator(t *testing.T) {
4445
"bar",
4546
"baz",
4647
),
48+
expErrors: 0,
4749
},
4850
"simple-mismatch": {
4951
in: types.String{Value: "foz"},
@@ -139,6 +141,7 @@ func TestOneOfValidator(t *testing.T) {
139141
"bar",
140142
"baz",
141143
),
144+
expErrors: 0,
142145
},
143146
"skip-validation-on-unknown": {
144147
in: types.String{Unknown: true},
@@ -148,6 +151,7 @@ func TestOneOfValidator(t *testing.T) {
148151
"bar",
149152
"baz",
150153
),
154+
expErrors: 0,
151155
},
152156
}
153157

stringvalidator/regex_matches.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (validator regexMatchesValidator) Validate(ctx context.Context, request tfs
3939
}
4040

4141
if ok := validator.regexp.MatchString(s); !ok {
42-
response.Diagnostics.Append(validatordiag.InvalidValueMatchDiagnostic(
42+
response.Diagnostics.Append(validatordiag.InvalidAttributeValueMatchDiagnostic(
4343
request.AttributePath,
4444
validator.Description(ctx),
4545
s,

stringvalidator/type_validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func validateString(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (string, bool) {
1313
t := request.AttributeConfig.Type(ctx)
1414
if t != types.StringType {
15-
response.Diagnostics.Append(validatordiag.InvalidTypeDiagnostic(
15+
response.Diagnostics.Append(validatordiag.InvalidAttributeTypeDiagnostic(
1616
request.AttributePath,
1717
"Expected value of type string",
1818
t.String(),

0 commit comments

Comments
 (0)