-
Notifications
You must be signed in to change notification settings - Fork 10
/
at_most.go
58 lines (47 loc) · 1.67 KB
/
at_most.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
// 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-validators/helpers/validatordiag"
)
var _ validator.Int64 = atMostValidator{}
// atMostValidator validates that an integer Attribute's value is at most a certain value.
type atMostValidator struct {
max int64
}
// Description describes the validation in plain text formatting.
func (validator atMostValidator) Description(_ context.Context) string {
return fmt.Sprintf("value must be at most %d", validator.max)
}
// MarkdownDescription describes the validation in Markdown formatting.
func (validator atMostValidator) MarkdownDescription(ctx context.Context) string {
return validator.Description(ctx)
}
// ValidateInt64 performs the validation.
func (v atMostValidator) ValidateInt64(ctx context.Context, request validator.Int64Request, response *validator.Int64Response) {
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
return
}
if request.ConfigValue.ValueInt64() > v.max {
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.Path,
v.Description(ctx),
fmt.Sprintf("%d", request.ConfigValue.ValueInt64()),
))
}
}
// AtMost returns an AttributeValidator which ensures that any configured
// attribute value:
//
// - Is a number, which can be represented by a 64-bit integer.
// - Is less than or equal to the given maximum.
//
// Null (unconfigured) and unknown (known after apply) values are skipped.
func AtMost(max int64) validator.Int64 {
return atMostValidator{
max: max,
}
}