This repository has been archived by the owner on Dec 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: string/int64 validator OneOfWithDescriptionIfAttributeIsOneOf
- Loading branch information
Showing
19 changed files
with
625 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# `OneOfWithDescription` | ||
|
||
!!! quote inline end "Released in v1.9.0" | ||
|
||
This validator allows to check if the string is one of the values of another attribute. | ||
It also allows to format the description and markdown description accordingly | ||
|
||
## How to use it | ||
|
||
```go | ||
// Schema defines the schema for the resource. | ||
func (r *xResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
(...) | ||
"foo": schema.StringAttribute{ | ||
Optional: true, | ||
MarkdownDescription: "foo ...", | ||
Validators: []validator.String{ | ||
fstringvalidator.OneOf("VM_NAME", "VM_TAGS"), | ||
}, | ||
}, | ||
"bar": schema.StringAttribute{ | ||
Optional: true, | ||
MarkdownDescription: "bar of ...", | ||
Validators: []validator.String{ | ||
fstringvalidator.OneOfWithDescriptionIfAttributeIsOneOf( | ||
path.MatchRelative().AtParent().AtName("foo"), | ||
[]attr.Value{types.StringValue("VM_NAME")}, | ||
func() []fstringvalidator.OneOfWithDescriptionIfAttributeIsOneOfValues { | ||
return []fstringvalidator.OneOfWithDescriptionIfAttributeIsOneOfValues{ | ||
{ | ||
Value: "CONTAINS", | ||
Description: "The `value` must be contained in the VM name.", | ||
}, | ||
{ | ||
Value: "STARTS_WITH", | ||
Description: "The VM name must start with the `value`.", | ||
}, | ||
{ | ||
Value: "ENDS_WITH", | ||
Description: "The VM name must end with the `value`.", | ||
}, | ||
{ | ||
Value: "EQUALS", | ||
Description: "The VM name must be equal to the `value`.", | ||
}, | ||
} | ||
}()...), | ||
}, | ||
}, | ||
``` | ||
## Description and Markdown description | ||
* **Description:** | ||
If the value of attribute <.type is "VM_NAME" the allowed values are : "CONTAINS" (The `value` must be contained in the VM name.), "STARTS_WITH" (The VM name must start with the `value`.), "ENDS_WITH" (The VM name must end with the `value`.), "EQUALS" (The VM name must be equal to the `value`.) | ||
* **Markdown description:** | ||
![oneofwithdescriptionifattributeisoneof](oneofwithdescriptionifattributeisoneof.png) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
int64validator/one_of_with_description_if_attribute_is_one_of.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package int64validator | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
|
||
"github.com/FrangipaneTeam/terraform-plugin-framework-validators/internal" | ||
) | ||
|
||
type OneOfWithDescriptionIfAttributeIsOneOfValues struct { | ||
Value int64 | ||
Description string | ||
} | ||
|
||
// OneOfWithDescriptionIfAttributeIsOneOf checks that the value is one of the expected values if the attribute is one of the exceptedValue. | ||
// The description of the value is used to generate advanced | ||
// Description and MarkdownDescription messages. | ||
func OneOfWithDescriptionIfAttributeIsOneOf(path path.Expression, exceptedValue []attr.Value, values ...OneOfWithDescriptionIfAttributeIsOneOfValues) validator.String { | ||
frameworkValues := make([]internal.OneOfWithDescriptionIfAttributeIsOneOf, 0, len(values)) | ||
|
||
for _, v := range values { | ||
frameworkValues = append(frameworkValues, internal.OneOfWithDescriptionIfAttributeIsOneOf{ | ||
Value: types.Int64Value(v.Value), | ||
Description: v.Description, | ||
}) | ||
} | ||
|
||
return internal.OneOfWithDescriptionIfAttributeIsOneOfValidator{ | ||
Values: frameworkValues, | ||
ExceptedValues: exceptedValue, | ||
PathExpression: path, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.