Skip to content
This repository was archived by the owner on Dec 30, 2024. It is now read-only.

Commit 76690f1

Browse files
authored
fix: bad description for OneOf and NoneOf validator (#46)
1 parent 5f3a042 commit 76690f1

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

.changelog/45.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:bug
2+
`validator/OneOf` - Fix description after upgrade dependency `hashicorp/terraform-plugin-framework-validators` to `v0.11.0`.
3+
```
4+
5+
```release-note:feature
6+
`validator/NoneOf` - Add support for formatting `NoneOf` validation description.
7+
```

schema.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@ import (
1616

1717
//go:generate go run template.go
1818
var (
19-
reOneOf = regexp.MustCompile(`\\"(\S+)\\"`)
19+
reOneOf = regexp.MustCompile(`\"(\S+)\"`)
2020
words = regexp.MustCompile(`(\w+)`)
2121
)
2222

2323
const (
24-
useStateForUnknown = "useStateForUnknownModifier"
25-
requireReplace = "requiresReplaceIfModifier"
26-
validatorOneOf = "oneOfValidator"
24+
useStateForUnknown = "useStateForUnknownModifier"
25+
requireReplace = "requiresReplaceIfModifier"
26+
validatorOneOf = "oneOfValidator"
27+
validatorOneOfCaseInsensitive = "oneOfCaseInsensitiveValidator"
28+
validatorNoneOf = "noneOfValidator"
29+
validatorNoneOfCaseInsensitive = "noneOfCaseInsensitiveValidator"
30+
31+
// ExactlyOneOfValidator have uppercase in the beginning of the name because it is a private type in terraform-plugin-framework.(https://github.com/hashicorp/terraform-plugin-framework-validators/blob/main/stringvalidator/exactly_one_of.go)
2732
validatorExactlyOneOf = "ExactlyOneOfValidator"
2833

2934
forceNewDesc = "(ForceNew)"
@@ -108,6 +113,21 @@ func addOneOfToDescription(oneof, description string) string {
108113
return newD
109114
}
110115

116+
// addNoneOfToDescription reformat NoneOf validator description.
117+
func addNoneOfToDescription(noneof, description string) string {
118+
params := reOneOf.FindAllStringSubmatch(noneof, -1)
119+
desc := ""
120+
for i, p := range params {
121+
desc += fmt.Sprintf("`%s`", p[1])
122+
if i < len(params)-1 {
123+
desc += ", "
124+
}
125+
}
126+
newD := description
127+
newD += "Value must not be one of : " + desc + "."
128+
return newD
129+
}
130+
111131
// addOnlyOneToDescription reformat OneOf validator description.
112132
func addOnlyOneToDescription(onlyO, description string) string {
113133
p := strings.Split(onlyO, ":")
@@ -172,8 +192,10 @@ func updateValidatorsDescription[D validator.Describer](ctx context.Context, val
172192
}
173193
name := getType(v)
174194
switch name {
175-
case validatorOneOf:
195+
case validatorOneOf, validatorOneOfCaseInsensitive:
176196
description = addOneOfToDescription(toAdd, description)
197+
case validatorNoneOf, validatorNoneOfCaseInsensitive:
198+
description = addNoneOfToDescription(toAdd, description)
177199
case validatorExactlyOneOf:
178200
description = addOnlyOneToDescription(toAdd, description)
179201
default:

0 commit comments

Comments
 (0)