diff --git a/pkg/report/validate.go b/pkg/report/validate.go index f4e059b09..8d6cff246 100644 --- a/pkg/report/validate.go +++ b/pkg/report/validate.go @@ -1,6 +1,12 @@ package report -import "strings" +import ( + "regexp" +) + +// Check for json, {{json }} and {{ json. }} which are not valid go template, +// {{json .}} is valid and thus not matched to let the template handle it like docker does. +var jsonRegex = regexp.MustCompile(`^\s*(json|{{\s*json\.?\s*}})\s*$`) // JSONFormat test CLI --format string to be a JSON request // @@ -8,5 +14,5 @@ import "strings" // ... process JSON and output // } func IsJSON(s string) bool { - return strings.TrimSpace(s) == "json" + return jsonRegex.MatchString(s) } diff --git a/pkg/report/validate_test.go b/pkg/report/validate_test.go index 3e21e4550..e8505f7e7 100644 --- a/pkg/report/validate_test.go +++ b/pkg/report/validate_test.go @@ -17,9 +17,12 @@ func TestIsJSON(t *testing.T) { {" json", true}, {" json ", true}, {" json ", true}, - {"{{json}}", false}, - {"{{json }}", false}, - {"{{json .}}", false}, + // special case, previous regex allowed this template string but it is not actually a valid template + {"{{json}}", true}, + {"{{json }}", true}, + {"{{json.}}", true}, + {"{{ json. }}", true}, + {"{{ json .}}", false}, {"{{ json . }}", false}, {" {{ json . }} ", false},