Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions util/templates/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,7 @@ func (t *Template) RenderResult(renderMode int, other map[string]any) ([]byte, m
}

// validate required fields from yaml
if s == "" && p.IsRequired() && !p.IsDeprecated() && (renderMode == RenderModeUnitTest ||
renderMode == RenderModeInstance && !testing.Testing()) {
if s == "" && p.IsRequired() && (renderMode == RenderModeUnitTest || renderMode == RenderModeInstance && !testing.Testing()) {
// validate required per usage
if len(p.Usages) == 0 || slices.Contains(p.Usages, usage) {
return nil, nil, fmt.Errorf("missing required `%s`", p.Name)
Expand Down
16 changes: 13 additions & 3 deletions util/templates/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (t *TextLanguage) Update(new TextLanguage, always bool) {
}

// MarshalJSON implements the json.Marshaler interface
func (t *TextLanguage) MarshalJSON() (out []byte, err error) {
func (t TextLanguage) MarshalJSON() ([]byte, error) {
mu.Lock()
s := t.String(encoderLanguage)
mu.Unlock()
Expand Down Expand Up @@ -232,7 +232,7 @@ func (p *Param) OverwriteProperties(withParam Param) {
}

func (p *Param) IsAdvanced() bool {
return p.Advanced
return p.Advanced && !p.Required
}

func (p *Param) IsMasked() bool {
Expand All @@ -244,7 +244,7 @@ func (p *Param) IsPrivate() bool {
}

func (p *Param) IsRequired() bool {
return p.Required
return p.Required && !p.Deprecated
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer to not introduce this logic but implement a build time check that catches this and fails our pipeline. We already have this for other aspects. I'll check ....

Copy link
Member

Choose a reason for hiding this comment

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

See here

// Validate that a param cannot be both masked and private
if p.Mask && p.Private {
return fmt.Errorf("param %s: 'mask' and 'private' cannot be used together. Use 'mask' for sensitive data like passwords/tokens that should be hidden in UI. Use 'private' for personal data like emails/locations that should only be redacted from bug reports", p.Name)
}
if p.Description.String("en") == "" || p.Description.String("de") == "" {
return fmt.Errorf("param %s: description can't be empty", p.Name)
}
maxLength := 50
actualLength := max(len(p.Description.String("en")), len(p.Description.String("de")))
if actualLength > maxLength {
return fmt.Errorf("param %s: description too long (%d/%d allowed)- use help instead", p.Name, actualLength, maxLength)
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Feel free to open separate PR and see how wide-spread the issue is.

}

func (p *Param) IsDeprecated() bool {
Expand All @@ -264,6 +264,16 @@ func (p *Param) yamlQuote(value string) string {
return yamlQuote(value)
}

var _ json.Marshaler = (*Param)(nil)

func (p Param) MarshalJSON() ([]byte, error) {
type param Param
pp := (param)(p)
pp.Required = p.IsRequired()
pp.Advanced = p.IsAdvanced()
return json.Marshal(pp)
}

// Product contains naming information about a product a template supports
type Product struct {
Brand string // product brand
Expand Down
77 changes: 77 additions & 0 deletions util/templates/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package templates

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestParamLogic(t *testing.T) {
{
p := Param{
Advanced: true, // omitempty
Required: true,
}
require.False(t, p.IsAdvanced(), "can't be advanced when required")

{
b, err := json.Marshal(p)
require.NoError(t, err)
assert.Equal(t, `{"Name":"","Description":"","Help":"","Required":true,"Type":"String"}`, string(b), "Marshal p advanced/required")
}
{
b, err := json.Marshal(&p)
require.NoError(t, err)
assert.Equal(t, `{"Name":"","Description":"","Help":"","Required":true,"Type":"String"}`, string(b), "Marshal *p advanced/required")
}
}

{
p := Param{
Deprecated: true,
Required: true, // omitempty
}
require.False(t, p.IsRequired(), "can't be required when deprecated")

{
b, err := json.Marshal(p)
require.NoError(t, err)
assert.Equal(t, `{"Name":"","Description":"","Help":"","Deprecated":true,"Type":"String"}`, string(b), "Marshal p deprecated/required")
}
{
b, err := json.Marshal(&p)
require.NoError(t, err)
assert.Equal(t, `{"Name":"","Description":"","Help":"","Deprecated":true,"Type":"String"}`, string(b), "Marshal *p deprecated/required")
}
}

b, err := json.Marshal(Param{
Deprecated: true,
Advanced: true, // omitempty
Required: true, // omitempty
})
require.NoError(t, err)
require.Equal(t, `{"Name":"","Description":"","Help":"","Deprecated":true,"Type":"String"}`, string(b))
}

func TestParamMarshal(t *testing.T) {
{
p := Param{
Description: TextLanguage{Generic: "foo"},
}

{
b, err := json.Marshal(p)
require.NoError(t, err)
assert.Equal(t, `{"Name":"","Description":"foo","Help":"","Type":"String"}`, string(b))
}

{
b, err := json.Marshal(&p)
require.NoError(t, err)
assert.Equal(t, `{"Name":"","Description":"foo","Help":"","Type":"String"}`, string(b))
}
}
}
Loading