Skip to content

Commit

Permalink
Fix template validation functions
Browse files Browse the repository at this point in the history
This commit adds the appropriate FuncMap to the template validation
functions.
  • Loading branch information
maraino committed Aug 3, 2023
1 parent 213ada0 commit 4aa6694
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 8 deletions.
6 changes: 1 addition & 5 deletions internal/templates/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@ import (
// results in invalid JSON, the template is invalid. When the template
// is valid, it can be used safely. A valid template can still result
// in invalid JSON when non-empty template data is provided.
func ValidateTemplate(data []byte) error {
func ValidateTemplate(data []byte, funcMap template.FuncMap) error {
if len(data) == 0 {
return nil
}

// get the default supported functions
var failMessage string
funcMap := GetFuncMap(&failMessage)

// prepare the template with our template functions
_, err := template.New("template").Funcs(funcMap).Parse(string(data))
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/templates/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ func TestValidateTemplate(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateTemplate(tt.data)
var failMessage string
err := ValidateTemplate(tt.data, GetFuncMap(&failMessage))
if tt.err != nil {
assert.Error(t, err)
assert.EqualError(t, err, tt.err.Error())
Expand Down
2 changes: 1 addition & 1 deletion sshutil/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (e *TemplateError) Error() string {

// ValidateTemplate validates a text template.
func ValidateTemplate(text []byte) error {
return templates.ValidateTemplate(text)
return templates.ValidateTemplate(text, GetFuncMap())
}

// ValidateTemplateData validates that template data is
Expand Down
2 changes: 1 addition & 1 deletion x509util/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (e *TemplateError) Error() string {

// ValidateTemplate validates a text template.
func ValidateTemplate(text []byte) error {
return templates.ValidateTemplate(text)
return templates.ValidateTemplate(text, GetFuncMap())
}

// ValidateTemplateData validates that template data is
Expand Down

0 comments on commit 4aa6694

Please sign in to comment.