Skip to content

Commit

Permalink
Add exported function to remove rules (#316)
Browse files Browse the repository at this point in the history
* feat(rules): Add way to remove rule

* feat(rules): Remove unnecessary change

* feat(rules): Fix linting error

* feat(rules): Fix linting error
  • Loading branch information
ldebruijn authored Oct 14, 2024
1 parent f0827b6 commit c888731
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ func AddRule(name string, ruleFunc RuleFunc) {
specifiedRules = append(specifiedRules, Rule{Name: name, RuleFunc: ruleFunc})
}

func RemoveRule(name string) {
var result []Rule // nolint:prealloc // using initialized with len(rules) produces a race condition
for _, r := range specifiedRules {
if r.Name == name {
continue
}
result = append(result, r)
}
specifiedRules = result
}

func Validate(schema *Schema, doc *QueryDocument, rules ...Rule) gqlerror.List {
if rules == nil {
rules = specifiedRules
Expand Down
10 changes: 10 additions & 0 deletions validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,13 @@ func TestCustomRuleSet(t *testing.T) {
require.Equal(t, "some error message", errList[0].Message)
require.Equal(t, "some other error message", errList[1].Message)
}

func TestRemoveRule(t *testing.T) {
// no error
validator.RemoveRule("rule that does not exist")

validator.AddRule("Rule that should no longer exist", func(observers *validator.Events, addError validator.AddErrFunc) {})

// no error
validator.RemoveRule("Rule that should no longer exist")
}

0 comments on commit c888731

Please sign in to comment.