Terraform
Custom Known Value Checks
Custom known value checks can be created by implementing the knownvalue.Check interface.
type Check interface {
CheckValue(value any) error
String() string
}
For example, a StringContains
implementation could look as follows:
var _ knownvalue.Check = stringContains{}
type stringContains struct {
value string
}
func (v stringContains) CheckValue(other any) error {
otherVal, ok := other.(string)
if !ok {
return fmt.Errorf("expected string value for StringContains check, got: %T", other)
}
if !strings.Contains(otherVal, v.value) {
return fmt.Errorf("expected string %q to contain %q for StringContains check", otherVal, v.value)
}
return nil
}
func (v stringContains) String() string {
return v.value
}
func StringContains(value string) stringContains {
return stringContains{
value: value,
}
}
CheckValue
Method Implementation
The other
parameter passed to the CheckValue
method is one of the following types:
- bool
- map[string]any
- []any
- string
Note: Numerical values will be of type json.Number
, with an underlying type of string
.
Refer to the following built-in known value checks for implementations that handle the different types that can be passed to the CheckValue
method in the other
parameter: