-
Notifications
You must be signed in to change notification settings - Fork 13
Add NoNullValues
validators
#246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1b2b865
listvalidator: add `NoNullValues` validator
jar-b daff222
setvalidator: add `NoNullValues` validator
jar-b c53ddcf
mapvalidator: add `NoNullValues` validator
jar-b 0a6078a
Documentation adjustments
austinvalle 3377de7
changelogs
austinvalle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,78 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package listvalidator | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
var _ validator.List = noNullValuesValidator{} | ||
var _ function.ListParameterValidator = noNullValuesValidator{} | ||
|
||
type noNullValuesValidator struct{} | ||
|
||
func (v noNullValuesValidator) Description(_ context.Context) string { | ||
return "null values are not permitted" | ||
} | ||
|
||
func (v noNullValuesValidator) MarkdownDescription(ctx context.Context) string { | ||
return v.Description(ctx) | ||
} | ||
|
||
func (v noNullValuesValidator) ValidateList(_ context.Context, req validator.ListRequest, resp *validator.ListResponse) { | ||
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() { | ||
return | ||
} | ||
|
||
elements := req.ConfigValue.Elements() | ||
|
||
for _, e := range elements { | ||
// Only evaluate known values for null | ||
if e.IsUnknown() { | ||
continue | ||
} | ||
|
||
if e.IsNull() { | ||
resp.Diagnostics.AddAttributeError( | ||
req.Path, | ||
"Null List Value", | ||
"This attribute contains a null value.", | ||
) | ||
} | ||
} | ||
} | ||
|
||
func (v noNullValuesValidator) ValidateParameterList(ctx context.Context, req function.ListParameterValidatorRequest, resp *function.ListParameterValidatorResponse) { | ||
if req.Value.IsNull() || req.Value.IsUnknown() { | ||
return | ||
} | ||
|
||
elements := req.Value.Elements() | ||
|
||
for _, e := range elements { | ||
// Only evaluate known values for null | ||
if e.IsUnknown() { | ||
continue | ||
} | ||
|
||
if e.IsNull() { | ||
resp.Error = function.ConcatFuncErrors( | ||
resp.Error, | ||
function.NewArgumentFuncError( | ||
req.ArgumentPosition, | ||
"Null List Value: This attribute contains a null value.", | ||
), | ||
) | ||
} | ||
} | ||
} | ||
|
||
// NoNullValues returns a validator which ensures that any configured list | ||
// only contains non-null values. | ||
func NoNullValues() noNullValuesValidator { | ||
return noNullValuesValidator{} | ||
} |
This file contains hidden or 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,42 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package listvalidator_test | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func ExampleNoNullValues() { | ||
// Used within a Schema method of a DataSource, Provider, or Resource | ||
_ = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"example_attr": schema.ListAttribute{ | ||
ElementType: types.StringType, | ||
Required: true, | ||
Validators: []validator.List{ | ||
// Validate this list must contain no null values. | ||
listvalidator.NoNullValues(), | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func ExampleNoNullValues_function() { | ||
_ = function.Definition{ | ||
Parameters: []function.Parameter{ | ||
function.ListParameter{ | ||
Name: "example_param", | ||
Validators: []function.ListParameterValidator{ | ||
// Validate this list must contain no null values. | ||
listvalidator.NoNullValues(), | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains hidden or 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,109 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package listvalidator_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator" | ||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func TestNoNullValuesValidator(t *testing.T) { | ||
t.Parallel() | ||
|
||
type testCase struct { | ||
val types.List | ||
expectError bool | ||
} | ||
tests := map[string]testCase{ | ||
"List unknown": { | ||
val: types.ListUnknown( | ||
types.StringType, | ||
), | ||
expectError: false, | ||
}, | ||
"List null": { | ||
val: types.ListNull( | ||
types.StringType, | ||
), | ||
expectError: false, | ||
}, | ||
"No null values": { | ||
val: types.ListValueMust( | ||
types.StringType, | ||
[]attr.Value{ | ||
types.StringValue("first"), | ||
types.StringValue("second"), | ||
}, | ||
), | ||
expectError: false, | ||
}, | ||
"Unknown value": { | ||
val: types.ListValueMust( | ||
types.StringType, | ||
[]attr.Value{ | ||
types.StringValue("first"), | ||
types.StringUnknown(), | ||
}, | ||
), | ||
expectError: false, | ||
}, | ||
"Null value": { | ||
val: types.ListValueMust( | ||
types.StringType, | ||
[]attr.Value{ | ||
types.StringValue("first"), | ||
types.StringNull(), | ||
}, | ||
), | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
t.Run(fmt.Sprintf("ValidateList - %s", name), func(t *testing.T) { | ||
t.Parallel() | ||
request := validator.ListRequest{ | ||
Path: path.Root("test"), | ||
PathExpression: path.MatchRoot("test"), | ||
ConfigValue: test.val, | ||
} | ||
response := validator.ListResponse{} | ||
listvalidator.NoNullValues().ValidateList(context.TODO(), request, &response) | ||
|
||
if !response.Diagnostics.HasError() && test.expectError { | ||
t.Fatal("expected error, got no error") | ||
} | ||
|
||
if response.Diagnostics.HasError() && !test.expectError { | ||
t.Fatalf("got unexpected error: %s", response.Diagnostics) | ||
} | ||
}) | ||
|
||
t.Run(fmt.Sprintf("ValidateParameterList - %s", name), func(t *testing.T) { | ||
t.Parallel() | ||
request := function.ListParameterValidatorRequest{ | ||
ArgumentPosition: 0, | ||
Value: test.val, | ||
} | ||
response := function.ListParameterValidatorResponse{} | ||
listvalidator.NoNullValues().ValidateParameterList(context.TODO(), request, &response) | ||
|
||
if response.Error == nil && test.expectError { | ||
t.Fatal("expected error, got no error") | ||
} | ||
|
||
if response.Error != nil && !test.expectError { | ||
t.Fatalf("got unexpected error: %s", response.Error) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or 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,78 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package mapvalidator | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
var _ validator.Map = noNullValuesValidator{} | ||
var _ function.MapParameterValidator = noNullValuesValidator{} | ||
|
||
type noNullValuesValidator struct{} | ||
|
||
func (v noNullValuesValidator) Description(_ context.Context) string { | ||
return "null values are not permitted" | ||
austinvalle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func (v noNullValuesValidator) MarkdownDescription(ctx context.Context) string { | ||
return v.Description(ctx) | ||
} | ||
|
||
func (v noNullValuesValidator) ValidateMap(_ context.Context, req validator.MapRequest, resp *validator.MapResponse) { | ||
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() { | ||
return | ||
} | ||
|
||
elements := req.ConfigValue.Elements() | ||
|
||
for _, e := range elements { | ||
// Only evaluate known values for null | ||
if e.IsUnknown() { | ||
continue | ||
} | ||
|
||
if e.IsNull() { | ||
resp.Diagnostics.AddAttributeError( | ||
req.Path, | ||
"Null Map Value", | ||
"This attribute contains a null value.", | ||
) | ||
} | ||
} | ||
} | ||
|
||
func (v noNullValuesValidator) ValidateParameterMap(ctx context.Context, req function.MapParameterValidatorRequest, resp *function.MapParameterValidatorResponse) { | ||
if req.Value.IsNull() || req.Value.IsUnknown() { | ||
return | ||
} | ||
|
||
elements := req.Value.Elements() | ||
|
||
for _, e := range elements { | ||
// Only evaluate known values for null | ||
if e.IsUnknown() { | ||
continue | ||
} | ||
|
||
if e.IsNull() { | ||
resp.Error = function.ConcatFuncErrors( | ||
resp.Error, | ||
function.NewArgumentFuncError( | ||
req.ArgumentPosition, | ||
"Null Map Value: This attribute contains a null value.", | ||
), | ||
) | ||
} | ||
} | ||
} | ||
|
||
// NoNullValues returns a validator which ensures that any configured map | ||
// only contains non-null values. | ||
func NoNullValues() noNullValuesValidator { | ||
return noNullValuesValidator{} | ||
} |
This file contains hidden or 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,42 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package mapvalidator_test | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/function" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func ExampleNoNullValues() { | ||
// Used within a Schema method of a DataSource, Provider, or Resource | ||
_ = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"example_attr": schema.MapAttribute{ | ||
ElementType: types.StringType, | ||
Required: true, | ||
Validators: []validator.Map{ | ||
// Validate this map must contain no null values. | ||
mapvalidator.NoNullValues(), | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func ExampleNoNullValues_function() { | ||
_ = function.Definition{ | ||
Parameters: []function.Parameter{ | ||
function.MapParameter{ | ||
Name: "example_param", | ||
Validators: []function.MapParameterValidator{ | ||
// Validate this map must contain no null values. | ||
mapvalidator.NoNullValues(), | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.