Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exported function to remove rules #316

Merged
merged 5 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}