Skip to content

Commit 30fbd49

Browse files
authored
Initial support for various other property validations (#30)
Reference: #19 Reference: #20 Reference: #21 Reference: #22 Reference: #26 Adds support for the following OAS properties: - `maximum` - `maxItems` - `maxLength` - `maxProperties` - `minimum` - `minItems` - `minLength` - `minProperties` - `pattern` - `uniqueItems`
1 parent fe37999 commit 30fbd49

19 files changed

Lines changed: 2131 additions & 96 deletions

DESIGN.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ If not required, or if the field is in a different schema than the `Read` operat
127127
| [description](https://spec.openapis.org/oas/latest.html#rich-text-formatting) | [(Attribute).MarkdownDescription](https://developer.hashicorp.com/terraform/plugin/framework/handling-data/schemas#markdowndescription-1) |
128128
| [enum](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-enum) | [(Attribute).Validators](https://developer.hashicorp.com/terraform/plugin/framework/validation) |
129129
| [format (password)](https://spec.openapis.org/oas/latest.html#data-types) | [(StringAttribute).Sensitive](https://developer.hashicorp.com/terraform/plugin/framework/handling-data/schemas#sensitive) |
130+
| [maximum](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-maximum) | [(Attribute).Validators](https://developer.hashicorp.com/terraform/plugin/framework/validation) |
131+
| [maxItems](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-maxItems) | [(Attribute).Validators](https://developer.hashicorp.com/terraform/plugin/framework/validation) |
132+
| [maxLength](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-maxLength) | [(StringAttribute).Validators](https://developer.hashicorp.com/terraform/plugin/framework/validation) |
133+
| [maxProperties](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-maxProperties) | [(MapAttribute).Validators](https://developer.hashicorp.com/terraform/plugin/framework/validation) |
134+
| [minimum](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-minimum) | [(Attribute).Validators](https://developer.hashicorp.com/terraform/plugin/framework/validation) |
135+
| [minItems](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-minItems) | [(Attribute).Validators](https://developer.hashicorp.com/terraform/plugin/framework/validation) |
136+
| [minLength](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-minLength) | [(StringAttribute).Validators](https://developer.hashicorp.com/terraform/plugin/framework/validation) |
137+
| [minProperties](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-minProperties) | [(Attribute).Validators](https://developer.hashicorp.com/terraform/plugin/framework/validation) |
138+
| [pattern](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-pattern) | [(StringAttribute).Validators](https://developer.hashicorp.com/terraform/plugin/framework/validation) |
139+
| [uniqueItems](https://json-schema.org/draft/2020-12/json-schema-validation.html#name-uniqueItems) | [(ListAttribute).Validators](https://developer.hashicorp.com/terraform/plugin/framework/validation) |
130140

131141
## Multi-type Support
132142

internal/mapper/frameworkvalidators/int64validator.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,62 @@ var (
2323
Int64ValidatorCodeImport code.Import = CodeImport(Int64ValidatorPackage)
2424
)
2525

26+
// Int64ValidatorAtLeast returns a custom validator mapped to the
27+
// int64validator package AtLeast function.
28+
func Int64ValidatorAtLeast(min int64) *schema.CustomValidator {
29+
var schemaDefinition strings.Builder
30+
31+
schemaDefinition.WriteString(Int64ValidatorPackage)
32+
schemaDefinition.WriteString(".AtLeast(")
33+
schemaDefinition.WriteString(strconv.FormatInt(min, 10))
34+
schemaDefinition.WriteString(")")
35+
36+
return &schema.CustomValidator{
37+
Imports: []code.Import{
38+
Int64ValidatorCodeImport,
39+
},
40+
SchemaDefinition: schemaDefinition.String(),
41+
}
42+
}
43+
44+
// Int64ValidatorAtMost returns a custom validator mapped to the
45+
// int64validator package AtMost function.
46+
func Int64ValidatorAtMost(max int64) *schema.CustomValidator {
47+
var schemaDefinition strings.Builder
48+
49+
schemaDefinition.WriteString(Int64ValidatorPackage)
50+
schemaDefinition.WriteString(".AtMost(")
51+
schemaDefinition.WriteString(strconv.FormatInt(max, 10))
52+
schemaDefinition.WriteString(")")
53+
54+
return &schema.CustomValidator{
55+
Imports: []code.Import{
56+
Int64ValidatorCodeImport,
57+
},
58+
SchemaDefinition: schemaDefinition.String(),
59+
}
60+
}
61+
62+
// Int64ValidatorBetween returns a custom validator mapped to the
63+
// int64validator package Between function.
64+
func Int64ValidatorBetween(min, max int64) *schema.CustomValidator {
65+
var schemaDefinition strings.Builder
66+
67+
schemaDefinition.WriteString(Int64ValidatorPackage)
68+
schemaDefinition.WriteString(".Between(")
69+
schemaDefinition.WriteString(strconv.FormatInt(min, 10))
70+
schemaDefinition.WriteString(", ")
71+
schemaDefinition.WriteString(strconv.FormatInt(max, 10))
72+
schemaDefinition.WriteString(")")
73+
74+
return &schema.CustomValidator{
75+
Imports: []code.Import{
76+
Int64ValidatorCodeImport,
77+
},
78+
SchemaDefinition: schemaDefinition.String(),
79+
}
80+
}
81+
2682
// Int64ValidatorOneOf returns a custom validator mapped to the int64validator
2783
// package OneOf function. If the values are nil or empty, nil is returned.
2884
func Int64ValidatorOneOf(values []int64) *schema.CustomValidator {

internal/mapper/frameworkvalidators/int64validator_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,113 @@ import (
1212
"github.com/hashicorp/terraform-plugin-codegen-spec/schema"
1313
)
1414

15+
func TestInt64ValidatorAtLeast(t *testing.T) {
16+
t.Parallel()
17+
18+
testCases := map[string]struct {
19+
min int64
20+
expected *schema.CustomValidator
21+
}{
22+
"test": {
23+
min: 123,
24+
expected: &schema.CustomValidator{
25+
Imports: []code.Import{
26+
{
27+
Path: "github.com/hashicorp/terraform-plugin-framework-validators/int64validator",
28+
},
29+
},
30+
SchemaDefinition: "int64validator.AtLeast(123)",
31+
},
32+
},
33+
}
34+
35+
for name, testCase := range testCases {
36+
name, testCase := name, testCase
37+
38+
t.Run(name, func(t *testing.T) {
39+
t.Parallel()
40+
41+
got := frameworkvalidators.Int64ValidatorAtLeast(testCase.min)
42+
43+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
44+
t.Errorf("unexpected difference: %s", diff)
45+
}
46+
})
47+
}
48+
}
49+
50+
func TestInt64ValidatorAtMost(t *testing.T) {
51+
t.Parallel()
52+
53+
testCases := map[string]struct {
54+
max int64
55+
expected *schema.CustomValidator
56+
}{
57+
"test": {
58+
max: 123,
59+
expected: &schema.CustomValidator{
60+
Imports: []code.Import{
61+
{
62+
Path: "github.com/hashicorp/terraform-plugin-framework-validators/int64validator",
63+
},
64+
},
65+
SchemaDefinition: "int64validator.AtMost(123)",
66+
},
67+
},
68+
}
69+
70+
for name, testCase := range testCases {
71+
name, testCase := name, testCase
72+
73+
t.Run(name, func(t *testing.T) {
74+
t.Parallel()
75+
76+
got := frameworkvalidators.Int64ValidatorAtMost(testCase.max)
77+
78+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
79+
t.Errorf("unexpected difference: %s", diff)
80+
}
81+
})
82+
}
83+
}
84+
85+
func TestInt64ValidatorBetween(t *testing.T) {
86+
t.Parallel()
87+
88+
testCases := map[string]struct {
89+
min int64
90+
max int64
91+
expected *schema.CustomValidator
92+
}{
93+
"test": {
94+
min: 123,
95+
max: 456,
96+
expected: &schema.CustomValidator{
97+
Imports: []code.Import{
98+
{
99+
Path: "github.com/hashicorp/terraform-plugin-framework-validators/int64validator",
100+
},
101+
},
102+
SchemaDefinition: "int64validator.Between(123, 456)",
103+
},
104+
},
105+
}
106+
107+
for name, testCase := range testCases {
108+
name, testCase := name, testCase
109+
110+
t.Run(name, func(t *testing.T) {
111+
t.Parallel()
112+
113+
got := frameworkvalidators.Int64ValidatorBetween(testCase.min, testCase.max)
114+
115+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
116+
t.Errorf("unexpected difference: %s", diff)
117+
}
118+
})
119+
}
120+
}
121+
15122
func TestInt64ValidatorOneOf(t *testing.T) {
16123
t.Parallel()
17124

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package frameworkvalidators
5+
6+
import (
7+
"strconv"
8+
"strings"
9+
10+
"github.com/hashicorp/terraform-plugin-codegen-spec/code"
11+
"github.com/hashicorp/terraform-plugin-codegen-spec/schema"
12+
)
13+
14+
const (
15+
// ListValidatorPackage is the name of the list validation package in
16+
// the framework validators module.
17+
ListValidatorPackage = "listvalidator"
18+
)
19+
20+
var (
21+
// ListValidatorCodeImport is a single allocation of the framework
22+
// validators module listvalidator package import.
23+
ListValidatorCodeImport code.Import = CodeImport(ListValidatorPackage)
24+
)
25+
26+
// ListValidatorSizeAtLeast returns a custom validator mapped to the
27+
// listvalidator package SizeAtLeast function.
28+
func ListValidatorSizeAtLeast(min int64) *schema.CustomValidator {
29+
var schemaDefinition strings.Builder
30+
31+
schemaDefinition.WriteString(ListValidatorPackage)
32+
schemaDefinition.WriteString(".SizeAtLeast(")
33+
schemaDefinition.WriteString(strconv.FormatInt(min, 10))
34+
schemaDefinition.WriteString(")")
35+
36+
return &schema.CustomValidator{
37+
Imports: []code.Import{
38+
ListValidatorCodeImport,
39+
},
40+
SchemaDefinition: schemaDefinition.String(),
41+
}
42+
}
43+
44+
// ListValidatorSizeAtMost returns a custom validator mapped to the
45+
// listvalidator package SizeAtMost function.
46+
func ListValidatorSizeAtMost(max int64) *schema.CustomValidator {
47+
var schemaDefinition strings.Builder
48+
49+
schemaDefinition.WriteString(ListValidatorPackage)
50+
schemaDefinition.WriteString(".SizeAtMost(")
51+
schemaDefinition.WriteString(strconv.FormatInt(max, 10))
52+
schemaDefinition.WriteString(")")
53+
54+
return &schema.CustomValidator{
55+
Imports: []code.Import{
56+
ListValidatorCodeImport,
57+
},
58+
SchemaDefinition: schemaDefinition.String(),
59+
}
60+
}
61+
62+
// ListValidatorSizeBetween returns a custom validator mapped to the
63+
// listvalidator package SizeBetween function.
64+
func ListValidatorSizeBetween(min, max int64) *schema.CustomValidator {
65+
var schemaDefinition strings.Builder
66+
67+
schemaDefinition.WriteString(ListValidatorPackage)
68+
schemaDefinition.WriteString(".SizeBetween(")
69+
schemaDefinition.WriteString(strconv.FormatInt(min, 10))
70+
schemaDefinition.WriteString(", ")
71+
schemaDefinition.WriteString(strconv.FormatInt(max, 10))
72+
schemaDefinition.WriteString(")")
73+
74+
return &schema.CustomValidator{
75+
Imports: []code.Import{
76+
ListValidatorCodeImport,
77+
},
78+
SchemaDefinition: schemaDefinition.String(),
79+
}
80+
}
81+
82+
// ListValidatorUniqueValues returns a custom validator mapped to the
83+
// listvalidator package UniqueValues function.
84+
func ListValidatorUniqueValues() *schema.CustomValidator {
85+
var schemaDefinition strings.Builder
86+
87+
schemaDefinition.WriteString(ListValidatorPackage)
88+
schemaDefinition.WriteString(".UniqueValues()")
89+
90+
return &schema.CustomValidator{
91+
Imports: []code.Import{
92+
ListValidatorCodeImport,
93+
},
94+
SchemaDefinition: schemaDefinition.String(),
95+
}
96+
}

0 commit comments

Comments
 (0)