-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Templates: error on missing required value #25932
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
Draft
andig
wants to merge
7
commits into
master
Choose a base branch
from
fix/tmpl-req
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
+44
−0
Conversation
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
Contributor
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.
Hey there - I've reviewed your changes - here's some feedback:
- The required-field check currently only runs when
val != nil, so a required parameter that is completely missing or explicitly set tonilwill not trigger an error; consider treatingnilas missing for required params as well. - Using
reflect.ValueOf(val).IsZero()on every render may be heavier than necessary; if the values are expected to be simple types (e.g., strings, numbers), you could replace reflection with type-specific zero checks to keep this hot path lighter.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The required-field check currently only runs when `val != nil`, so a required parameter that is completely missing or explicitly set to `nil` will not trigger an error; consider treating `nil` as missing for required params as well.
- Using `reflect.ValueOf(val).IsZero()` on every render may be heavier than necessary; if the values are expected to be simple types (e.g., strings, numbers), you could replace reflection with type-specific zero checks to keep this hot path lighter.
## Individual Comments
### Comment 1
<location> `util/templates/template_test.go:48-50` </location>
<code_context>
+ },
+ }
+
+ _, _, err := tmpl.RenderResult(0, map[string]any{
+ "Param": "foo",
+ })
+ require.NoError(t, err)
+
</code_context>
<issue_to_address>
**suggestion (testing):** Assert on the error message content for the failing required case
Since the implementation returns a specific error message (e.g., `missing required \`param\``) when required validation fails, the test should assert on that message using `require.ErrorContains` or `require.EqualError`, not just that an error occurred. This will better guard against regressions where required-field validation changes or is removed.
Suggested implementation:
```golang
func TestRequired(t *testing.T) {
tmpl := &Template{
TemplateDefinition: TemplateDefinition{
Params: []Param{
{
Name: "param",
Required: true,
},
},
},
}
_, _, err := tmpl.RenderResult(0, map[string]any{})
require.ErrorContains(t, err, "missing required `param`")
_, _, err = tmpl.RenderResult(0, map[string]any{
"Param": "foo",
})
require.NoError(t, err)
}
```
If the actual error string produced by the implementation differs (e.g., different wording or punctuation), update `"missing required \`param\`"` in the test to exactly match the real error text or a stable substring of it. Also, if the initial failing call currently uses a different argument (e.g., `nil` instead of `map[string]any{}`), apply the `require.ErrorContains` assertion at that call site without changing the arguments unless necessary.
</issue_to_address>
### Comment 2
<location> `util/templates/template_test.go:37-45` </location>
<code_context>
}
+
+func TestRequired(t *testing.T) {
+ tmpl := &Template{
+ TemplateDefinition: TemplateDefinition{
+ Params: []Param{
+ {
+ Name: "param",
+ Required: true,
+ },
+ },
+ },
+ }
+
</code_context>
<issue_to_address>
**question (testing):** Clarify or test the key casing behavior between `Param` and `param`
The template param is named `"param"`, but the test passes `"Param"` in the map. If case-insensitive matching is intended, please make that explicit (e.g., a dedicated `TestRequired_CaseInsensitiveKey` or a short comment). If not, align the `Name` and map key to avoid the test accidentally depending on unspecified behavior.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Member
Author
|
@naltatis with this change UI tests are failing due to missing required fields. The errors are real and probably due to faulty test cases. Could you take a look if it would be feasible to fix the tests? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fix #25930
TODO