Skip to content

Commit ac6932d

Browse files
authored
all: Add Go documentation examples for validators (#62)
Reference: #47
1 parent 2299f55 commit ac6932d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1097
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package float64validator_test
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
5+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
6+
"github.com/hashicorp/terraform-plugin-framework/types"
7+
)
8+
9+
func ExampleAtLeast() {
10+
// Used within a GetSchema method of a DataSource, Provider, or Resource
11+
_ = tfsdk.Schema{
12+
Attributes: map[string]tfsdk.Attribute{
13+
"example_attr": {
14+
Required: true,
15+
Type: types.Float64Type,
16+
Validators: []tfsdk.AttributeValidator{
17+
// Validate floating point value must be at least 42.42
18+
float64validator.AtLeast(42.42),
19+
},
20+
},
21+
},
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package float64validator_test
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
5+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
6+
"github.com/hashicorp/terraform-plugin-framework/types"
7+
)
8+
9+
func ExampleAtMost() {
10+
// Used within a GetSchema method of a DataSource, Provider, or Resource
11+
_ = tfsdk.Schema{
12+
Attributes: map[string]tfsdk.Attribute{
13+
"example_attr": {
14+
Required: true,
15+
Type: types.Float64Type,
16+
Validators: []tfsdk.AttributeValidator{
17+
// Validate floating point value must be at most 42.42
18+
float64validator.AtMost(42.42),
19+
},
20+
},
21+
},
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package float64validator_test
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
5+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
6+
"github.com/hashicorp/terraform-plugin-framework/types"
7+
)
8+
9+
func ExampleBetween() {
10+
// Used within a GetSchema method of a DataSource, Provider, or Resource
11+
_ = tfsdk.Schema{
12+
Attributes: map[string]tfsdk.Attribute{
13+
"example_attr": {
14+
Required: true,
15+
Type: types.Float64Type,
16+
Validators: []tfsdk.AttributeValidator{
17+
// Validate floating point value must be at least 0.0 and at most 1.0
18+
float64validator.Between(0.0, 1.0),
19+
},
20+
},
21+
},
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package float64validator_test
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
5+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
6+
"github.com/hashicorp/terraform-plugin-framework/types"
7+
)
8+
9+
func ExampleNoneOf() {
10+
// Used within a GetSchema method of a DataSource, Provider, or Resource
11+
_ = tfsdk.Schema{
12+
Attributes: map[string]tfsdk.Attribute{
13+
"example_attr": {
14+
Required: true,
15+
Type: types.Float64Type,
16+
Validators: []tfsdk.AttributeValidator{
17+
// Validate floating point value must not be 1.2, 2.4, or 4.8
18+
float64validator.NoneOf([]float64{1.2, 2.4, 4.8}...),
19+
},
20+
},
21+
},
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package float64validator_test
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
5+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
6+
"github.com/hashicorp/terraform-plugin-framework/types"
7+
)
8+
9+
func ExampleOneOf() {
10+
// Used within a GetSchema method of a DataSource, Provider, or Resource
11+
_ = tfsdk.Schema{
12+
Attributes: map[string]tfsdk.Attribute{
13+
"example_attr": {
14+
Required: true,
15+
Type: types.Float64Type,
16+
Validators: []tfsdk.AttributeValidator{
17+
// Validate floating point value must be 1.2, 2.4, or 4.8
18+
float64validator.OneOf([]float64{1.2, 2.4, 4.8}...),
19+
},
20+
},
21+
},
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package int64validator_test
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
5+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
6+
"github.com/hashicorp/terraform-plugin-framework/types"
7+
)
8+
9+
func ExampleAtLeast() {
10+
// Used within a GetSchema method of a DataSource, Provider, or Resource
11+
_ = tfsdk.Schema{
12+
Attributes: map[string]tfsdk.Attribute{
13+
"example_attr": {
14+
Required: true,
15+
Type: types.Int64Type,
16+
Validators: []tfsdk.AttributeValidator{
17+
// Validate integer value must be at least 42
18+
int64validator.AtLeast(42),
19+
},
20+
},
21+
},
22+
}
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package int64validator_test
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
5+
"github.com/hashicorp/terraform-plugin-framework/path"
6+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
7+
"github.com/hashicorp/terraform-plugin-framework/types"
8+
)
9+
10+
func ExampleAtLeastSumOf() {
11+
// Used within a GetSchema method of a DataSource, Provider, or Resource
12+
_ = tfsdk.Schema{
13+
Attributes: map[string]tfsdk.Attribute{
14+
"example_attr": {
15+
Required: true,
16+
Type: types.Int64Type,
17+
Validators: []tfsdk.AttributeValidator{
18+
// Validate this integer value must be at least the
19+
// summed integer values of other_attr1 and other_attr2.
20+
int64validator.AtLeastSumOf(path.Expressions{
21+
path.MatchRoot("other_attr1"),
22+
path.MatchRoot("other_attr2"),
23+
}...),
24+
},
25+
},
26+
"other_attr1": {
27+
Required: true,
28+
Type: types.Int64Type,
29+
},
30+
"other_attr2": {
31+
Required: true,
32+
Type: types.Int64Type,
33+
},
34+
},
35+
}
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package int64validator_test
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
5+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
6+
"github.com/hashicorp/terraform-plugin-framework/types"
7+
)
8+
9+
func ExampleAtMost() {
10+
// Used within a GetSchema method of a DataSource, Provider, or Resource
11+
_ = tfsdk.Schema{
12+
Attributes: map[string]tfsdk.Attribute{
13+
"example_attr": {
14+
Required: true,
15+
Type: types.Int64Type,
16+
Validators: []tfsdk.AttributeValidator{
17+
// Validate integer value must be at most 42
18+
int64validator.AtMost(42),
19+
},
20+
},
21+
},
22+
}
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package int64validator_test
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
5+
"github.com/hashicorp/terraform-plugin-framework/path"
6+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
7+
"github.com/hashicorp/terraform-plugin-framework/types"
8+
)
9+
10+
func ExampleAtMostSumOf() {
11+
// Used within a GetSchema method of a DataSource, Provider, or Resource
12+
_ = tfsdk.Schema{
13+
Attributes: map[string]tfsdk.Attribute{
14+
"example_attr": {
15+
Required: true,
16+
Type: types.Int64Type,
17+
Validators: []tfsdk.AttributeValidator{
18+
// Validate this integer value must be at most the
19+
// summed integer values of other_attr1 and other_attr2.
20+
int64validator.AtMostSumOf(path.Expressions{
21+
path.MatchRoot("other_attr1"),
22+
path.MatchRoot("other_attr2"),
23+
}...),
24+
},
25+
},
26+
"other_attr1": {
27+
Required: true,
28+
Type: types.Int64Type,
29+
},
30+
"other_attr2": {
31+
Required: true,
32+
Type: types.Int64Type,
33+
},
34+
},
35+
}
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package int64validator_test
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
5+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
6+
"github.com/hashicorp/terraform-plugin-framework/types"
7+
)
8+
9+
func ExampleBetween() {
10+
// Used within a GetSchema method of a DataSource, Provider, or Resource
11+
_ = tfsdk.Schema{
12+
Attributes: map[string]tfsdk.Attribute{
13+
"example_attr": {
14+
Required: true,
15+
Type: types.Int64Type,
16+
Validators: []tfsdk.AttributeValidator{
17+
// Validate integer value must be at least 10 and at most 100
18+
int64validator.Between(10, 100),
19+
},
20+
},
21+
},
22+
}
23+
}

0 commit comments

Comments
 (0)