-
Notifications
You must be signed in to change notification settings - Fork 10
/
one_of.go
63 lines (48 loc) · 1.53 KB
/
one_of.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package int64validator
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
)
var _ validator.Int64 = oneOfValidator{}
// oneOfValidator validates that the value matches one of expected values.
type oneOfValidator struct {
values []types.Int64
}
func (v oneOfValidator) Description(ctx context.Context) string {
return v.MarkdownDescription(ctx)
}
func (v oneOfValidator) MarkdownDescription(_ context.Context) string {
return fmt.Sprintf("value must be one of: %q", v.values)
}
func (v oneOfValidator) ValidateInt64(ctx context.Context, request validator.Int64Request, response *validator.Int64Response) {
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
return
}
value := request.ConfigValue
for _, otherValue := range v.values {
if value.Equal(otherValue) {
return
}
}
response.Diagnostics.Append(validatordiag.InvalidAttributeValueMatchDiagnostic(
request.Path,
v.Description(ctx),
value.String(),
))
}
// OneOf checks that the Int64 held in the attribute
// is one of the given `values`.
func OneOf(values ...int64) validator.Int64 {
frameworkValues := make([]types.Int64, 0, len(values))
for _, value := range values {
frameworkValues = append(frameworkValues, types.Int64Value(value))
}
return oneOfValidator{
values: frameworkValues,
}
}