diff --git a/validator/validator.go b/validator/validator.go index e145e570..7955b761 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -1,6 +1,8 @@ package validator import ( + "sort" + . "github.com/dgraph-io/gqlparser/v2/ast" "github.com/dgraph-io/gqlparser/v2/gqlerror" ) @@ -11,7 +13,9 @@ type ruleFunc func(observers *Events, addError AddErrFunc) type rule struct { name string - rule ruleFunc + // rules will be called in the ascending order + order int + rule ruleFunc } var rules []rule @@ -22,10 +26,19 @@ func AddRule(name string, f ruleFunc) { rules = append(rules, rule{name: name, rule: f}) } +// AddRuleWithOrder to rule set with an order. +// f is called once each time `Validate` is executed. +func AddRuleWithOrder(name string, order int, f ruleFunc) { + rules = append(rules, rule{name: name, order: order, rule: f}) +} + func Validate(schema *Schema, doc *QueryDocument, variables map[string]interface{}) gqlerror.List { var errs gqlerror.List observers := &Events{} + sort.Slice(rules, func(i, j int) bool { + return rules[i].order < rules[j].order + }) for i := range rules { rule := rules[i] rule.rule(observers, func(options ...ErrorOption) {