Skip to content

Commit be3051b

Browse files
committed
bail -> onError
1 parent cea8a25 commit be3051b

File tree

3 files changed

+36
-46
lines changed

3 files changed

+36
-46
lines changed

src/validation/ValidationContext.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ import { Kind } from '../language/kinds';
88
import { type ASTVisitor, visit, visitWithTypeInfo } from '../language/visitor';
99
import {
1010
type DocumentNode,
11+
type FragmentDefinitionNode,
12+
type FragmentSpreadNode,
1113
type OperationDefinitionNode,
12-
type VariableNode,
1314
type SelectionSetNode,
14-
type FragmentSpreadNode,
15-
type FragmentDefinitionNode,
15+
type VariableNode,
1616
} from '../language/ast';
1717

1818
import { type GraphQLSchema } from '../type/schema';
1919
import { type GraphQLDirective } from '../type/directives';
2020
import {
21-
type GraphQLInputType,
22-
type GraphQLOutputType,
21+
type GraphQLArgument,
2322
type GraphQLCompositeType,
2423
type GraphQLField,
25-
type GraphQLArgument,
24+
type GraphQLInputType,
25+
type GraphQLOutputType,
2626
} from '../type/definition';
2727

2828
import { TypeInfo } from '../utilities/TypeInfo';
@@ -34,16 +34,14 @@ type VariableUsage = {|
3434
+defaultValue: ?mixed,
3535
|};
3636

37-
export class ValidationBailEarlyError extends Error {}
38-
3937
/**
4038
* An instance of this class is passed as the "this" context to all validators,
4139
* allowing access to commonly useful contextual information from within a
4240
* validation rule.
4341
*/
4442
export class ASTValidationContext {
4543
_ast: DocumentNode;
46-
_bail: boolean;
44+
_onError: ?(err: Error) => void = undefined;
4745
_errors: Array<GraphQLError>;
4846
_fragments: ?ObjMap<FragmentDefinitionNode>;
4947
_fragmentSpreads: Map<SelectionSetNode, $ReadOnlyArray<FragmentSpreadNode>>;
@@ -52,19 +50,21 @@ export class ASTValidationContext {
5250
$ReadOnlyArray<FragmentDefinitionNode>,
5351
>;
5452

55-
constructor(ast: DocumentNode, bail: boolean = false): void {
53+
constructor(ast: DocumentNode, onError?: (err: Error) => void): void {
5654
this._ast = ast;
57-
this._bail = bail;
5855
this._errors = [];
5956
this._fragments = undefined;
6057
this._fragmentSpreads = new Map();
6158
this._recursivelyReferencedFragments = new Map();
59+
if (onError) {
60+
this._onError = onError.bind(this);
61+
}
6262
}
6363

6464
reportError(error: GraphQLError): void {
6565
this._errors.push(error);
66-
if (this._bail) {
67-
throw new ValidationBailEarlyError();
66+
if (this._onError) {
67+
this._onError(error);
6868
}
6969
}
7070

@@ -172,9 +172,9 @@ export class ValidationContext extends ASTValidationContext {
172172
schema: GraphQLSchema,
173173
ast: DocumentNode,
174174
typeInfo: TypeInfo,
175-
bail: boolean = false,
175+
onError?: (err: Error) => void,
176176
): void {
177-
super(ast, bail);
177+
super(ast, onError);
178178
this._schema = schema;
179179
this._typeInfo = typeInfo;
180180
this._variableUsages = new Map();

src/validation/__tests__/validation-test.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('Validate: Supports full validation', () => {
7575
]);
7676
});
7777

78-
it('exits validation early when bail option is set', () => {
78+
it('properly calls onError callback when passed', () => {
7979
const doc = parse(`
8080
query {
8181
cat {
@@ -89,21 +89,14 @@ describe('Validate: Supports full validation', () => {
8989
}
9090
`);
9191

92-
// Ensure that the number of errors without the bail option is 2
93-
const errors = validate(testSchema, doc, specifiedRules);
94-
expect(errors).to.be.length(2);
92+
const expectedNumberOfErrors = 2;
93+
let errorCount = 0;
94+
validate(testSchema, doc, specifiedRules, undefined, (err, ctx) => {
95+
expect(err).to.not.be.a('null');
96+
expect(ctx).to.not.be.a('null');
97+
expect(ctx.getErrors()).to.be.length(++errorCount);
98+
});
9599

96-
const bailedErrors = validate(
97-
testSchema,
98-
doc,
99-
specifiedRules,
100-
undefined,
101-
true,
102-
);
103-
expect(bailedErrors).to.be.length(1);
104-
const errorMessages = errors.map(err => err.message);
105-
expect(errorMessages[0]).to.equal(
106-
'Cannot query field "someNonExistentField" on type "Cat".',
107-
);
100+
expect(errorCount).to.be.equal(expectedNumberOfErrors);
108101
});
109102
});

src/validation/validate.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ import { TypeInfo } from '../utilities/TypeInfo';
1414

1515
import { specifiedRules, specifiedSDLRules } from './specifiedRules';
1616
import {
17-
type SDLValidationRule,
18-
type ValidationRule,
1917
SDLValidationContext,
18+
type SDLValidationRule,
2019
ValidationContext,
21-
ValidationBailEarlyError,
20+
type ValidationRule,
2221
} from './ValidationContext';
2322

2423
/**
@@ -42,26 +41,24 @@ export function validate(
4241
documentAST: DocumentNode,
4342
rules?: $ReadOnlyArray<ValidationRule> = specifiedRules,
4443
typeInfo?: TypeInfo = new TypeInfo(schema),
45-
bail?: boolean = false,
44+
onError?: (err: Error, ctx: ValidationContext) => void,
4645
): $ReadOnlyArray<GraphQLError> {
4746
devAssert(documentAST, 'Must provide document');
4847
// If the schema used for validation is invalid, throw an error.
4948
assertValidSchema(schema);
5049

51-
const context = new ValidationContext(schema, documentAST, typeInfo, bail);
50+
const context = new ValidationContext(schema, documentAST, typeInfo, function(
51+
err,
52+
) {
53+
if (onError) {
54+
onError(err, this);
55+
}
56+
});
5257
// This uses a specialized visitor which runs multiple visitors in parallel,
5358
// while maintaining the visitor skip and break API.
5459
const visitor = visitInParallel(rules.map(rule => rule(context)));
55-
try {
56-
// Visit the whole document with each instance of all provided rules.
57-
visit(documentAST, visitWithTypeInfo(typeInfo, visitor));
58-
} catch (e) {
59-
// If the caught error is not a `ValidationBailEarlyError`, rethrow as the
60-
// error is fatal
61-
if (!(e instanceof ValidationBailEarlyError)) {
62-
throw e;
63-
}
64-
}
60+
// Visit the whole document with each instance of all provided rules.
61+
visit(documentAST, visitWithTypeInfo(typeInfo, visitor));
6562
return context.getErrors();
6663
}
6764

0 commit comments

Comments
 (0)