fix: error if rule has no regex or path

This commit is contained in:
Henrique Dias
2024-11-28 16:57:10 +01:00
parent d418bd2661
commit 64bbdc7b15
2 changed files with 36 additions and 10 deletions
+22
View File
@@ -192,6 +192,26 @@ cors:
}
func TestConfigRules(t *testing.T) {
t.Run("Only Regex or Path", func(t *testing.T) {
content := `
directory: /
rules:
- regex: '^.+\.js$'
path: /public/access/`
writeAndParseConfigWithError(t, content, ".yaml", "cannot define both regex and path")
})
t.Run("Regex or Path Required", func(t *testing.T) {
content := `
directory: /
rules:
- permissions: CRUD`
writeAndParseConfigWithError(t, content, ".yaml", "must either define a path of a regex")
})
t.Run("Parse", func(t *testing.T) {
content := `
directory: /
rules:
@@ -210,6 +230,8 @@ rules:
require.NotEmpty(t, cfg.Rules[1].Path)
require.Nil(t, cfg.Rules[1].Regex)
})
}
func TestConfigEnv(t *testing.T) {
+4
View File
@@ -16,6 +16,10 @@ type Rule struct {
}
func (r *Rule) Validate() error {
if r.Regex == nil && r.Path == "" {
return errors.New("invalid rule: must either define a path of a regex")
}
if r.Regex != nil && r.Path != "" {
return errors.New("invalid rule: cannot define both regex and path")
}