Skip to content

Adding Map validation for KeysAre and ValuesAre #38

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 5 commits into from
Jun 23, 2022

Conversation

bendbennett
Copy link
Contributor

Closes: #13

@bendbennett bendbennett requested a review from a team as a code owner June 21, 2022 13:08
for k := range elems {
request := tfsdk.ValidateAttributeRequest{
AttributePath: req.AttributePath.WithElementKeyString(k),
AttributeConfig: types.String{Value: k},
Copy link
Contributor Author

@bendbennett bendbennett Jun 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit "hacky" to me. If a validator that is subsequently called by ranging over v.keyValidators uses the AttributePath to obtain the value for performing the validation this will break. Interested to hear opinions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is something to call in the Go documentation for the validator/function, but expected in this usage. The intention of the validator is to loop through the string map keys to perform string validations against those keys and the ValuesAre validator is separate for this reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the docs to provide some clarification around this usage.

@bendbennett
Copy link
Contributor Author

Following discussion with @detro we could consolidate the ValuesAre validators for List, Map and Set into a single generic validator if we don't require any type-specific validation.

@bflad bflad added this to the v0.3.0 milestone Jun 22, 2022
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 🚀

keysAreValidators: []tfsdk.AttributeValidator{
stringvalidator.LengthAtLeast(4),
},
expectError: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: At some point it might be nice to refactor these validator tests to test the expected error messages, rather than just being conditional that some error was raised.

gotMapElems, gotOk := validateMap(context.Background(), testCase.request, &tfsdk.ValidateAttributeResponse{})

if diff := cmp.Diff(gotMapElems, testCase.expectedMap); diff != "" {
t.Errorf("unexpected float64 difference: %s", diff)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Copypasta typo 🍝


for k := range elems {
request := tfsdk.ValidateAttributeRequest{
AttributePath: req.AttributePath.WithElementKeyString(k),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent choice here to highlight the whole key/value (at least I think it'll highlight the key too, but regardless, we do not have a way to signal upstream just a map key so this is a good compromise rather than just highlighting the whole map).

@bflad bflad added enhancement New feature or request type/map types.Map validators labels Jun 22, 2022
@bendbennett bendbennett merged commit 1a3ea19 into main Jun 23, 2022
@bendbennett bendbennett deleted the bendbennett/issues-13 branch June 23, 2022 11:54
bflad added a commit that referenced this pull request Jun 29, 2022
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request type/map types.Map validators
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Map Element Validation (KeysAre / ValuesAre)
2 participants