/
validation-test.ts
176 lines (151 loc) · 4.52 KB
/
validation-test.ts
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
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { GraphQLError } from '../../error/GraphQLError';
import type { DirectiveNode } from '../../language/ast';
import { parse } from '../../language/parser';
import { TypeInfo } from '../../utilities/TypeInfo';
import { buildSchema } from '../../utilities/buildASTSchema';
import type { ValidationContext } from '../ValidationContext';
import { validate } from '../validate';
import { testSchema } from './harness';
describe('Validate: Supports full validation', () => {
it('rejects invalid documents', () => {
// @ts-expect-error (expects a DocumentNode as a second parameter)
expect(() => validate(testSchema, null)).to.throw('Must provide document.');
});
it('validates queries', () => {
const doc = parse(`
query {
catOrDog {
... on Cat {
furColor
}
... on Dog {
isHouseTrained
}
}
}
`);
const errors = validate(testSchema, doc);
expect(errors).to.deep.equal([]);
});
it('detects unknown fields', () => {
const doc = parse(`
{
unknown
}
`);
const errors = validate(testSchema, doc);
expect(errors).to.deep.equal([
{
locations: [{ line: 3, column: 9 }],
message: 'Cannot query field "unknown" on type "QueryRoot".',
},
]);
});
it('Deprecated: validates using a custom TypeInfo', () => {
// This TypeInfo will never return a valid field.
const typeInfo = new TypeInfo(testSchema, null, () => null);
const doc = parse(`
query {
catOrDog {
... on Cat {
furColor
}
... on Dog {
isHouseTrained
}
}
}
`);
const errors = validate(testSchema, doc, undefined, undefined, typeInfo);
const errorMessages = errors.map((error) => error.message);
expect(errorMessages).to.deep.equal([
'Cannot query field "catOrDog" on type "QueryRoot". Did you mean "catOrDog"?',
'Cannot query field "furColor" on type "Cat". Did you mean "furColor"?',
'Cannot query field "isHouseTrained" on type "Dog". Did you mean "isHouseTrained"?',
]);
});
it('validates using a custom rule', () => {
const schema = buildSchema(`
directive @custom(arg: String) on FIELD
type Query {
foo: String
}
`);
const doc = parse(`
query {
name @custom
}
`);
function customRule(context: ValidationContext) {
return {
Directive(node: DirectiveNode) {
const directiveDef = context.getDirective();
const error = new GraphQLError(
'Reporting directive: ' + String(directiveDef),
node,
);
context.reportError(error);
},
};
}
const errors = validate(schema, doc, [customRule]);
expect(errors).to.deep.equal([
{
message: 'Reporting directive: @custom',
locations: [{ line: 3, column: 14 }],
},
]);
});
});
describe('Validate: Limit maximum number of validation errors', () => {
const query = `
{
firstUnknownField
secondUnknownField
thirdUnknownField
}
`;
const doc = parse(query, { noLocation: true });
function validateDocument(options: { maxErrors?: number }) {
return validate(testSchema, doc, undefined, options);
}
function invalidFieldError(fieldName: string) {
return {
message: `Cannot query field "${fieldName}" on type "QueryRoot".`,
locations: [],
};
}
it('when maxErrors is equal to number of errors', () => {
const errors = validateDocument({ maxErrors: 3 });
expect(errors).to.be.deep.equal([
invalidFieldError('firstUnknownField'),
invalidFieldError('secondUnknownField'),
invalidFieldError('thirdUnknownField'),
]);
});
it('when maxErrors is less than number of errors', () => {
const errors = validateDocument({ maxErrors: 2 });
expect(errors).to.be.deep.equal([
invalidFieldError('firstUnknownField'),
invalidFieldError('secondUnknownField'),
{
message:
'Too many validation errors, error limit reached. Validation aborted.',
},
]);
});
it('passthrough exceptions from rules', () => {
function customRule() {
return {
Field() {
throw new Error('Error from custom rule!');
},
};
}
expect(() =>
validate(testSchema, doc, [customRule], { maxErrors: 1 }),
).to.throw(/^Error from custom rule!$/);
});
});