From ac52e99d3c12baf4520abee76eaf70e5f800d459 Mon Sep 17 00:00:00 2001 From: Lars de Bruijn <9264036+ldebruijn@users.noreply.github.com> Date: Thu, 12 Sep 2024 23:54:45 -0700 Subject: [PATCH 1/4] feat(rules): Add way to remove rule --- validator/validator.go | 13 ++++++++++++- validator/validator_test.go | 10 ++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/validator/validator.go b/validator/validator.go index b4f37ce2..cdc1c247 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -17,12 +17,23 @@ type rule struct { var rules []rule -// addRule to rule set. +// addRule to initRule set. // f is called once each time `Validate` is executed. func AddRule(name string, f ruleFunc) { rules = append(rules, rule{name: name, rule: f}) } +func RemoveRule(name string) { + var result []rule + for _, r := range rules { + if r.name == name { + continue + } + result = append(result, r) + } + rules = result +} + func Validate(schema *Schema, doc *QueryDocument) gqlerror.List { var errs gqlerror.List if schema == nil { diff --git a/validator/validator_test.go b/validator/validator_test.go index 50b6ebc2..5988dbca 100644 --- a/validator/validator_test.go +++ b/validator/validator_test.go @@ -138,3 +138,13 @@ func TestNoUnusedVariables(t *testing.T) { require.Nil(t, validator.Validate(s, q)) }) } + +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") +} From 629b9bf50763c581330c94790789fbf6a6cc9f26 Mon Sep 17 00:00:00 2001 From: Lars de Bruijn <9264036+ldebruijn@users.noreply.github.com> Date: Thu, 12 Sep 2024 23:56:04 -0700 Subject: [PATCH 2/4] feat(rules): Remove unnecessary change --- validator/validator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator/validator.go b/validator/validator.go index cdc1c247..eba7460a 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -17,7 +17,7 @@ type rule struct { var rules []rule -// addRule to initRule set. +// addRule to rule set. // f is called once each time `Validate` is executed. func AddRule(name string, f ruleFunc) { rules = append(rules, rule{name: name, rule: f}) From bfe24f4ddcfc5fad10685182b4be34776fd842ba Mon Sep 17 00:00:00 2001 From: Lars de Bruijn <9264036+ldebruijn@users.noreply.github.com> Date: Fri, 13 Sep 2024 08:29:49 -0700 Subject: [PATCH 3/4] feat(rules): Fix linting error --- validator/validator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator/validator.go b/validator/validator.go index eba7460a..a9cc5e28 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -24,7 +24,7 @@ func AddRule(name string, f ruleFunc) { } func RemoveRule(name string) { - var result []rule + result := make([]rule, len(rules)) for _, r := range rules { if r.name == name { continue From 70bd43e5a4799a55a675802c38c970556df2c9ac Mon Sep 17 00:00:00 2001 From: Lars de Bruijn <9264036+ldebruijn@users.noreply.github.com> Date: Fri, 13 Sep 2024 08:41:06 -0700 Subject: [PATCH 4/4] feat(rules): Fix linting error --- validator/rules/fields_on_correct_type.go | 2 +- validator/rules/fragments_on_composite_types.go | 4 ++-- validator/validator.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/validator/rules/fields_on_correct_type.go b/validator/rules/fields_on_correct_type.go index 24d4f3db..aea4fd61 100644 --- a/validator/rules/fields_on_correct_type.go +++ b/validator/rules/fields_on_correct_type.go @@ -27,7 +27,7 @@ func init() { } addError( - Message(message), + Message(message), // nolint:govet At(field.Position), ) }) diff --git a/validator/rules/fragments_on_composite_types.go b/validator/rules/fragments_on_composite_types.go index 81ef861b..37adf6d4 100644 --- a/validator/rules/fragments_on_composite_types.go +++ b/validator/rules/fragments_on_composite_types.go @@ -20,7 +20,7 @@ func init() { message := fmt.Sprintf(`Fragment cannot condition on non composite type "%s".`, inlineFragment.TypeCondition) addError( - Message(message), + Message(message), // nolint:govet At(inlineFragment.Position), ) }) @@ -33,7 +33,7 @@ func init() { message := fmt.Sprintf(`Fragment "%s" cannot condition on non composite type "%s".`, fragment.Name, fragment.TypeCondition) addError( - Message(message), + Message(message), // nolint:govet At(fragment.Position), ) }) diff --git a/validator/validator.go b/validator/validator.go index a9cc5e28..1b5c80ad 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -24,7 +24,7 @@ func AddRule(name string, f ruleFunc) { } func RemoveRule(name string) { - result := make([]rule, len(rules)) + var result []rule // nolint:prealloc // using initialized with len(rules) produces a race condition for _, r := range rules { if r.name == name { continue