...

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

Documentation: github.com/vektah/gqlparser/v2/validator

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

View as plain text