...

Source file src/github.com/vektah/gqlparser/validator/validator.go

Documentation: github.com/vektah/gqlparser/validator

     1  package validator
     2  
     3  import (
     4  	. "github.com/vektah/gqlparser/ast"
     5  	"github.com/vektah/gqlparser/gqlerror"
     6  )
     7  
     8  type AddErrFunc func(options ...ErrorOption)
     9  
    10  type ruleFunc func(observers *Events, addError AddErrFunc)
    11  
    12  type rule struct {
    13  	name string
    14  	rule ruleFunc
    15  }
    16  
    17  var rules []rule
    18  
    19  // addRule to rule set.
    20  // f is called once each time `Validate` is executed.
    21  func AddRule(name string, f ruleFunc) {
    22  	rules = append(rules, rule{name: name, rule: f})
    23  }
    24  
    25  func Validate(schema *Schema, doc *QueryDocument) gqlerror.List {
    26  	var errs gqlerror.List
    27  
    28  	observers := &Events{}
    29  	for i := range rules {
    30  		rule := rules[i]
    31  		rule.rule(observers, func(options ...ErrorOption) {
    32  			err := &gqlerror.Error{
    33  				Rule: rule.name,
    34  			}
    35  			for _, o := range options {
    36  				o(err)
    37  			}
    38  			errs = append(errs, err)
    39  		})
    40  	}
    41  
    42  	Walk(schema, doc, observers)
    43  	return errs
    44  }
    45  

View as plain text