Skip to content

Commit

Permalink
Merge pull request #17 from dgraph-io/aman/add_order_to_rules
Browse files Browse the repository at this point in the history
chore: adding order to the validation rules
  • Loading branch information
aman-bansal authored Aug 31, 2021
2 parents 7914f56 + 40ef820 commit 4b0b96a
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion validator/validator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package validator

import (
"sort"

. "github.com/dgraph-io/gqlparser/v2/ast"
"github.com/dgraph-io/gqlparser/v2/gqlerror"
)
Expand All @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit 4b0b96a

Please sign in to comment.