-
Notifications
You must be signed in to change notification settings - Fork 233
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
Add various Map key and value validators #304
Conversation
@appilon Sorry for the delay in responding, must have missed the e-mail.
or |
Hello @ewbankkit sorry for the delayed back and forth. I see now the value of these validators. I wanted to share with you though we are working on V2 of the SDK and one of the main enhancements is Diagnostic support #368 . Validators for maps would be greatly enhanced using the new validation API being introduced in that PR. Would you be interested in landing this in V2 instead against the new function signature? I can work with you on adapting the implementation. Or do you feel demand for these validators to land and be available in V1 of the SDK? Either way we can get these in, and if needed forward ported or back ported. |
@appilon Thanks for the response. No, there's no need to get these into v1 of the SDK as we have been OK this long without them 😄. Yes, I'd be interested in adapting for v2. |
@ewbankkit awesome! The PR is likely opaque so feel free to send me any questions on that PR or this one. I will try and update the description of it to show how to define a new ValidateDiagFunc within the next 24h |
Yes @ewbankkit please rebase off that branch, it will be merged very soon and then you can rebase off version2! |
@appilon I had a go at converting the first of the new functions,
|
len := len(key) | ||
if len < min || len > max { | ||
return diag.Diagnostics{{ | ||
Severity: diag.Error, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in my question answering it might be a better pattern to declare var diags diag.Diagnostics
at the top and append to the slice, returning diags
at the end. You could exit at the first error or actually accumulate all of them to return. Of course in situations where you cannot continue you should exit early, but in situations such as this were we can actually successfully collect multiple errors you have the ability to do so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now I'm exiting at the first error but it would make sense to accumulate a Diagnostic
for every key (in this function) that is in error.
This will complicate the Equals
helper or expanded equivalent as for example map iteration is non-deterministic and the Diagnostics
entries will potentially be in different orders.
OK, I think I've just talked myself into sorting the keys and then iterating the map in a known order and accumulating all errors - The user's experience will be better in this case.
helper/validation/map_test.go
Outdated
t.Fatalf("%s: wrong number of diags, expected %d, got %d", tn, len(tc.ExpectedDiags), len(diags)) | ||
} | ||
for j := range diags { | ||
if diags[j].Severity != tc.ExpectedDiags[j].Severity { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seeing test cases around diags makes me think it should have a .Equals()
helper. to reduce boilerplate like this for devs. You could PR that helper in with this PR if you like. Or I can add it and you rebase.
Great @ewbankkit I'll answer each question:
An empty slice of
The SDK doesn't always agree with itself 😂 but yes I believe that's fine to just do a type assert (no ok check), Go will panic if the type isn't right which should be a signal to file a bug with the SDK.
I'm not sure what you meant by this? Are you referring to the tests?
Summary is the bolded text rendered after |
return func(v interface{}, path cty.Path) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
|
||
for _, key := range sortedKeys(v.(map[string]interface{})) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could sort here or just sort in the check/test instead. Having a deterministic user experience for the diagnostics might be nice though so sorting here is certainly appropriate.
} | ||
} | ||
|
||
func checkDiagnostics(t *testing.T, tn string, got, expected diag.Diagnostics) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added this for now. As you mention, maybe replace with a method on Diagnostics
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is good enough for now
I've converted all 4 functions and tests. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great Work!
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
Add various validators for Map keys and values:
MapKeyLenBetween
validates that the provided value is of type map and the length of all keys are betweenmin
andmax
MapValueLenBetween
validates that the provided value is of type map and the length of all values are betweenmin
andmax
MapKeyMatch
validates that the provided value is of type map and all keys match a given regexMapValueMatch
validates that the provided value is of type map and all values match a given regex