diff --git a/validator/validator.go b/validator/validator.go index 36564b2..fb4db19 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -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 diff --git a/validator/validator_test.go b/validator/validator_test.go index 6c1b50b..db2fde6 100644 --- a/validator/validator_test.go +++ b/validator/validator_test.go @@ -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") +}