From 5c57388f2d0c800223d50a8c032c5c9ae8c26d03 Mon Sep 17 00:00:00 2001 From: 2ynn Date: Mon, 26 Oct 2026 11:46:02 -0400 Subject: [PATCH] chore: check config field validation patterns compile in Go Every target's gen.yaml config fields may declare a ValidationRegex in config.ts. The patterns are authored as JavaScript regular expressions but the Speakeasy CLI compiles them with Go's regexp.MustCompile, so a JavaScript-only construct would panic the CLI, and a default value that fails its own pattern would reject a freshly initialised gen.yaml. Add a test over all available templates that compiles each pattern the way the CLI does and checks each default value against it. --- pkg/templates/config_test.go | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 pkg/templates/config_test.go diff --git a/pkg/templates/config_test.go b/pkg/templates/config_test.go new file mode 100644 index 000000000..eb55be3ec --- /dev/null +++ b/pkg/templates/config_test.go @@ -0,0 +1,49 @@ +package templates_test + +import ( + "fmt" + "regexp" + "strings" + "testing" + + "github.com/speakeasy-api/openapi-generation/v2/internal/types" + "github.com/speakeasy-api/openapi-generation/v2/pkg/templates" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// RegExp.source emits "/" as the Unicode escape below, which the CLI rewrites before compiling the pattern with Go's regexp. +const escapedSlash = "\x5cu002f" + +func TestConfigFieldValidationRegexes(t *testing.T) { + t.Parallel() + + for _, name := range templates.GetAvailableTemplates() { + t.Run(name, func(t *testing.T) { + t.Parallel() + + for _, newSDK := range []bool{false, true} { + fields, err := templates.GetLanguageConfigFields(types.NewTargetFromTemplate(name), newSDK) + require.NoError(t, err) + + for _, field := range fields { + if field.ValidationRegex == nil { + continue + } + + pattern := strings.ReplaceAll(*field.ValidationRegex, escapedSlash, "/") + regex, err := regexp.Compile(pattern) + require.NoError(t, err, "field %q (newSDK=%v)", field.Name, newSDK) + + if field.DefaultValue == nil { + continue + } + + defaultValue := fmt.Sprintf("%v", *field.DefaultValue) + assert.True(t, regex.MatchString(defaultValue), + "field %q (newSDK=%v): default %q does not match %s", field.Name, newSDK, defaultValue, pattern) + } + } + }) + } +}