forked from graphql-go/graphql
/
validator.go
212 lines (188 loc) · 6.01 KB
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package graphql
import (
"github.com/graphql-go/graphql/gqlerrors"
"github.com/graphql-go/graphql/language/ast"
"github.com/graphql-go/graphql/language/kinds"
"github.com/graphql-go/graphql/language/visitor"
)
type ValidationResult struct {
IsValid bool
Errors []gqlerrors.FormattedError
}
func ValidateDocument(schema *Schema, astDoc *ast.Document, rules []ValidationRuleFn) (vr ValidationResult) {
if len(rules) == 0 {
rules = SpecifiedRules
}
vr.IsValid = false
if schema == nil {
vr.Errors = append(vr.Errors, gqlerrors.NewFormattedError("Must provide schema"))
return vr
}
if astDoc == nil {
vr.Errors = append(vr.Errors, gqlerrors.NewFormattedError("Must provide document"))
return vr
}
vr.Errors = visitUsingRules(schema, astDoc, rules)
if len(vr.Errors) == 0 {
vr.IsValid = true
}
return vr
}
func visitUsingRules(schema *Schema, astDoc *ast.Document, rules []ValidationRuleFn) (errors []gqlerrors.FormattedError) {
typeInfo := NewTypeInfo(schema)
context := NewValidationContext(schema, astDoc, typeInfo)
var visitInstance func(astNode ast.Node, instance *ValidationRuleInstance)
visitInstance = func(astNode ast.Node, instance *ValidationRuleInstance) {
visitor.Visit(astNode, &visitor.VisitorOptions{
Enter: func(p visitor.VisitFuncParams) (string, interface{}) {
var action = visitor.ActionNoChange
var result interface{}
switch node := p.Node.(type) {
case ast.Node:
// Collect type information about the current position in the AST.
typeInfo.Enter(node)
// Do not visit top level fragment definitions if this instance will
// visit those fragments inline because it
// provided `visitSpreadFragments`.
kind := node.GetKind()
if kind == kinds.FragmentDefinition &&
p.Key != nil && instance.VisitSpreadFragments == true {
return visitor.ActionSkip, nil
}
// Get the visitor function from the validation instance, and if it
// exists, call it with the visitor arguments.
enterFn := visitor.GetVisitFn(instance.VisitorOpts, false, kind)
if enterFn != nil {
action, result = enterFn(p)
}
// If the visitor returned an error, log it and do not visit any
// deeper nodes.
if err, ok := result.(error); ok && err != nil {
errors = append(errors, gqlerrors.FormatError(err))
action = visitor.ActionSkip
}
if err, ok := result.([]error); ok && err != nil {
errors = append(errors, gqlerrors.FormatErrors(err...)...)
action = visitor.ActionSkip
}
// If any validation instances provide the flag `visitSpreadFragments`
// and this node is a fragment spread, visit the fragment definition
// from this point.
if action == visitor.ActionNoChange && result == nil &&
instance.VisitSpreadFragments == true && kind == kinds.FragmentSpread {
node, _ := node.(*ast.FragmentSpread)
name := node.Name
nameVal := ""
if name != nil {
nameVal = name.Value
}
fragment := context.Fragment(nameVal)
if fragment != nil {
visitInstance(fragment, instance)
}
}
// If the result is "false" (ie action === Action.Skip), we're not visiting any descendent nodes,
// but need to update typeInfo.
if action == visitor.ActionSkip {
typeInfo.Leave(node)
}
}
return action, result
},
Leave: func(p visitor.VisitFuncParams) (string, interface{}) {
var action = visitor.ActionNoChange
var result interface{}
switch node := p.Node.(type) {
case ast.Node:
kind := node.GetKind()
// Get the visitor function from the validation instance, and if it
// exists, call it with the visitor arguments.
leaveFn := visitor.GetVisitFn(instance.VisitorOpts, true, kind)
if leaveFn != nil {
action, result = leaveFn(p)
}
// If the visitor returned an error, log it and do not visit any
// deeper nodes.
if err, ok := result.(error); ok && err != nil {
errors = append(errors, gqlerrors.FormatError(err))
action = visitor.ActionSkip
}
if err, ok := result.([]error); ok && err != nil {
errors = append(errors, gqlerrors.FormatErrors(err...)...)
action = visitor.ActionSkip
}
// Update typeInfo.
typeInfo.Leave(node)
}
return action, result
},
}, nil)
}
instances := []*ValidationRuleInstance{}
for _, rule := range rules {
instance := rule(context)
instances = append(instances, instance)
}
for _, instance := range instances {
visitInstance(astDoc, instance)
}
return errors
}
type ValidationContext struct {
schema *Schema
astDoc *ast.Document
typeInfo *TypeInfo
fragments map[string]*ast.FragmentDefinition
}
func NewValidationContext(schema *Schema, astDoc *ast.Document, typeInfo *TypeInfo) *ValidationContext {
return &ValidationContext{
schema: schema,
astDoc: astDoc,
typeInfo: typeInfo,
}
}
func (ctx *ValidationContext) Schema() *Schema {
return ctx.schema
}
func (ctx *ValidationContext) Document() *ast.Document {
return ctx.astDoc
}
func (ctx *ValidationContext) Fragment(name string) *ast.FragmentDefinition {
if len(ctx.fragments) == 0 {
if ctx.Document() == nil {
return nil
}
defs := ctx.Document().Definitions
fragments := map[string]*ast.FragmentDefinition{}
for _, def := range defs {
if def, ok := def.(*ast.FragmentDefinition); ok {
defName := ""
if def.Name != nil {
defName = def.Name.Value
}
fragments[defName] = def
}
}
ctx.fragments = fragments
}
f, _ := ctx.fragments[name]
return f
}
func (ctx *ValidationContext) Type() Output {
return ctx.typeInfo.Type()
}
func (ctx *ValidationContext) ParentType() Composite {
return ctx.typeInfo.ParentType()
}
func (ctx *ValidationContext) InputType() Input {
return ctx.typeInfo.InputType()
}
func (ctx *ValidationContext) FieldDef() *FieldDefinition {
return ctx.typeInfo.FieldDef()
}
func (ctx *ValidationContext) Directive() *Directive {
return ctx.typeInfo.Directive()
}
func (ctx *ValidationContext) Argument() *Argument {
return ctx.typeInfo.Argument()
}