44 "context"
55 "testing"
66
7- "github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
87 "github.com/hashicorp/terraform-plugin-framework-validators/schemavalidator"
8+ "github.com/hashicorp/terraform-plugin-framework/path"
99 "github.com/hashicorp/terraform-plugin-framework/tfsdk"
1010 "github.com/hashicorp/terraform-plugin-framework/types"
1111 "github.com/hashicorp/terraform-plugin-go/tftypes"
@@ -16,15 +16,16 @@ func TestAtLeastOneOfValidator(t *testing.T) {
1616
1717 type testCase struct {
1818 req tfsdk.ValidateAttributeRequest
19- in [] * tftypes. AttributePath
19+ in path. Expressions
2020 expErrors int
2121 }
2222
2323 testCases := map [string ]testCase {
2424 "base" : {
2525 req : tfsdk.ValidateAttributeRequest {
26- AttributeConfig : types.String {Value : "bar value" },
27- AttributePath : tftypes .NewAttributePath ().WithAttributeName ("bar" ),
26+ AttributeConfig : types.String {Value : "bar value" },
27+ AttributePath : path .Root ("bar" ),
28+ AttributePathExpression : path .MatchRoot ("bar" ),
2829 Config : tfsdk.Config {
2930 Schema : tfsdk.Schema {
3031 Attributes : map [string ]tfsdk.Attribute {
@@ -47,14 +48,15 @@ func TestAtLeastOneOfValidator(t *testing.T) {
4748 }),
4849 },
4950 },
50- in : [] * tftypes. AttributePath {
51- tftypes . NewAttributePath (). WithAttributeName ("foo" ),
51+ in : path. Expressions {
52+ path . MatchRoot ("foo" ),
5253 },
5354 },
5455 "self-is-null" : {
5556 req : tfsdk.ValidateAttributeRequest {
56- AttributeConfig : types.String {Null : true },
57- AttributePath : tftypes .NewAttributePath ().WithAttributeName ("bar" ),
57+ AttributeConfig : types.String {Null : true },
58+ AttributePath : path .Root ("bar" ),
59+ AttributePathExpression : path .MatchRoot ("bar" ),
5860 Config : tfsdk.Config {
5961 Schema : tfsdk.Schema {
6062 Attributes : map [string ]tfsdk.Attribute {
@@ -77,14 +79,15 @@ func TestAtLeastOneOfValidator(t *testing.T) {
7779 }),
7880 },
7981 },
80- in : [] * tftypes. AttributePath {
81- tftypes . NewAttributePath (). WithAttributeName ("foo" ),
82+ in : path. Expressions {
83+ path . MatchRoot ("foo" ),
8284 },
8385 },
8486 "error_none-set" : {
8587 req : tfsdk.ValidateAttributeRequest {
86- AttributeConfig : types.String {Value : "bar value" },
87- AttributePath : tftypes .NewAttributePath ().WithAttributeName ("bar" ),
88+ AttributeConfig : types.String {Value : "bar value" },
89+ AttributePath : path .Root ("bar" ),
90+ AttributePathExpression : path .MatchRoot ("bar" ),
8891 Config : tfsdk.Config {
8992 Schema : tfsdk.Schema {
9093 Attributes : map [string ]tfsdk.Attribute {
@@ -112,16 +115,17 @@ func TestAtLeastOneOfValidator(t *testing.T) {
112115 }),
113116 },
114117 },
115- in : [] * tftypes. AttributePath {
116- tftypes . NewAttributePath (). WithAttributeName ("foo" ),
117- tftypes . NewAttributePath (). WithAttributeName ("baz" ),
118+ in : path. Expressions {
119+ path . MatchRoot ("foo" ),
120+ path . MatchRoot ("baz" ),
118121 },
119122 expErrors : 1 ,
120123 },
121124 "multiple-set" : {
122125 req : tfsdk.ValidateAttributeRequest {
123- AttributeConfig : types.String {Value : "bar value" },
124- AttributePath : tftypes .NewAttributePath ().WithAttributeName ("bar" ),
126+ AttributeConfig : types.String {Value : "bar value" },
127+ AttributePath : path .Root ("bar" ),
128+ AttributePathExpression : path .MatchRoot ("bar" ),
125129 Config : tfsdk.Config {
126130 Schema : tfsdk.Schema {
127131 Attributes : map [string ]tfsdk.Attribute {
@@ -149,15 +153,16 @@ func TestAtLeastOneOfValidator(t *testing.T) {
149153 }),
150154 },
151155 },
152- in : [] * tftypes. AttributePath {
153- tftypes . NewAttributePath (). WithAttributeName ("foo" ),
154- tftypes . NewAttributePath (). WithAttributeName ("baz" ),
156+ in : path. Expressions {
157+ path . MatchRoot ("foo" ),
158+ path . MatchRoot ("baz" ),
155159 },
156160 },
157161 "allow-duplicate-input" : {
158162 req : tfsdk.ValidateAttributeRequest {
159- AttributeConfig : types.String {Value : "bar value" },
160- AttributePath : tftypes .NewAttributePath ().WithAttributeName ("bar" ),
163+ AttributeConfig : types.String {Value : "bar value" },
164+ AttributePath : path .Root ("bar" ),
165+ AttributePathExpression : path .MatchRoot ("bar" ),
161166 Config : tfsdk.Config {
162167 Schema : tfsdk.Schema {
163168 Attributes : map [string ]tfsdk.Attribute {
@@ -185,16 +190,17 @@ func TestAtLeastOneOfValidator(t *testing.T) {
185190 }),
186191 },
187192 },
188- in : [] * tftypes. AttributePath {
189- tftypes . NewAttributePath (). WithAttributeName ("foo" ),
190- tftypes . NewAttributePath (). WithAttributeName ("bar" ),
191- tftypes . NewAttributePath (). WithAttributeName ("baz" ),
193+ in : path. Expressions {
194+ path . MatchRoot ("foo" ),
195+ path . MatchRoot ("bar" ),
196+ path . MatchRoot ("baz" ),
192197 },
193198 },
194199 "unknowns" : {
195200 req : tfsdk.ValidateAttributeRequest {
196- AttributeConfig : types.String {Value : "bar value" },
197- AttributePath : tftypes .NewAttributePath ().WithAttributeName ("bar" ),
201+ AttributeConfig : types.String {Value : "bar value" },
202+ AttributePath : path .Root ("bar" ),
203+ AttributePathExpression : path .MatchRoot ("bar" ),
198204 Config : tfsdk.Config {
199205 Schema : tfsdk.Schema {
200206 Attributes : map [string ]tfsdk.Attribute {
@@ -222,11 +228,43 @@ func TestAtLeastOneOfValidator(t *testing.T) {
222228 }),
223229 },
224230 },
225- in : [] * tftypes. AttributePath {
226- tftypes . NewAttributePath (). WithAttributeName ("foo" ),
227- tftypes . NewAttributePath (). WithAttributeName ("baz" ),
231+ in : path. Expressions {
232+ path . MatchRoot ("foo" ),
233+ path . MatchRoot ("baz" ),
228234 },
229235 },
236+ "matches-no-attribute-in-schema" : {
237+ req : tfsdk.ValidateAttributeRequest {
238+ AttributeConfig : types.String {Value : "bar value" },
239+ AttributePath : path .Root ("bar" ),
240+ AttributePathExpression : path .MatchRoot ("bar" ),
241+ Config : tfsdk.Config {
242+ Schema : tfsdk.Schema {
243+ Attributes : map [string ]tfsdk.Attribute {
244+ "foo" : {
245+ Type : types .Int64Type ,
246+ },
247+ "bar" : {
248+ Type : types .StringType ,
249+ },
250+ },
251+ },
252+ Raw : tftypes .NewValue (tftypes.Object {
253+ AttributeTypes : map [string ]tftypes.Type {
254+ "foo" : tftypes .Number ,
255+ "bar" : tftypes .String ,
256+ },
257+ }, map [string ]tftypes.Value {
258+ "foo" : tftypes .NewValue (tftypes .Number , 42 ),
259+ "bar" : tftypes .NewValue (tftypes .String , nil ),
260+ }),
261+ },
262+ },
263+ in : path.Expressions {
264+ path .MatchRoot ("fooz" ),
265+ },
266+ expErrors : 1 ,
267+ },
230268 }
231269
232270 for name , test := range testCases {
@@ -239,12 +277,12 @@ func TestAtLeastOneOfValidator(t *testing.T) {
239277 t .Fatal ("expected error(s), got none" )
240278 }
241279
242- if test .expErrors > 0 && test .expErrors != validatordiag . ErrorsCount ( res .Diagnostics ) {
243- t .Fatalf ("expected %d error(s), got %d: %v" , test .expErrors , validatordiag . ErrorsCount ( res .Diagnostics ), res .Diagnostics )
280+ if test .expErrors > 0 && test .expErrors != res .Diagnostics . ErrorsCount ( ) {
281+ t .Fatalf ("expected %d error(s), got %d: %v" , test .expErrors , res .Diagnostics . ErrorsCount ( ), res .Diagnostics )
244282 }
245283
246284 if test .expErrors == 0 && res .Diagnostics .HasError () {
247- t .Fatalf ("expected no error(s), got %d: %v" , validatordiag . ErrorsCount ( res .Diagnostics ), res .Diagnostics )
285+ t .Fatalf ("expected no error(s), got %d: %v" , res .Diagnostics . ErrorsCount ( ), res .Diagnostics )
248286 }
249287 })
250288 }
0 commit comments