Skip to content

Commit 048b2a7

Browse files
committed
fix: validate regex in artifact name input
Catch invalid regex patterns (e.g. [unclosed) at input time rather than letting them silently end up in the policy file.
1 parent 0a8ac1a commit 048b2a7

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

internal/policywizard/forms.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package policywizard
33
import (
44
"fmt"
55
"path/filepath"
6+
"regexp"
67
"slices"
78
"strings"
89

@@ -144,7 +145,13 @@ func (m *Model) buildForm() *huh.Form {
144145
))
145146

146147
case stepExprArtifactName:
147-
f = inputForm("value", "Artifact name regex", "e.g. ^datadog:.*", "^datadog:.*", "regex")
148+
f = huh.NewForm(huh.NewGroup(
149+
huh.NewInput().Key("value").
150+
Title("Artifact name regex").
151+
Description("e.g. ^datadog:.*").
152+
Placeholder("^datadog:.*").
153+
Validate(validateRegex),
154+
))
148155

149156
case stepExprCustomCtx:
150157
f = huh.NewForm(huh.NewGroup(
@@ -239,6 +246,16 @@ func notEmpty(field string) func(string) error {
239246
}
240247
}
241248

249+
func validateRegex(s string) error {
250+
if s == "" {
251+
return fmt.Errorf("regex is required")
252+
}
253+
if _, err := regexp.Compile(s); err != nil {
254+
return fmt.Errorf("invalid regex: %s", err)
255+
}
256+
return nil
257+
}
258+
242259
func validateYAMLExtension(s string) error {
243260
if s == "" {
244261
return nil // placeholder "policy.yaml" will be used

0 commit comments

Comments
 (0)