diff --git a/src/type/__tests__/validation-test.ts b/src/type/__tests__/validation-test.ts index c7e3a15449..d5b8773700 100644 --- a/src/type/__tests__/validation-test.ts +++ b/src/type/__tests__/validation-test.ts @@ -425,6 +425,23 @@ describe('Type System: A Schema must have Object root types', () => { }, ]); }); + + it('rejects a Schema whose directives have empty locations', () => { + const badDirective = new GraphQLDirective({ + name: 'BadDirective', + args: {}, + locations: [], + }); + const schema = new GraphQLSchema({ + query: SomeObjectType, + directives: [badDirective], + }); + expectJSON(validateSchema(schema)).toDeepEqual([ + { + message: 'Directive @BadDirective must include 1 or more locations.', + }, + ]); + }); }); describe('Type System: Objects must have fields', () => { diff --git a/src/type/validate.ts b/src/type/validate.ts index 94ef92d05b..56ad63fc64 100644 --- a/src/type/validate.ts +++ b/src/type/validate.ts @@ -172,7 +172,12 @@ function validateDirectives(context: SchemaValidationContext): void { // Ensure they are named correctly. validateName(context, directive); - // TODO: Ensure proper locations. + if (directive.locations.length === 0) { + context.reportError( + `Directive @${directive.name} must include 1 or more locations.`, + directive.astNode, + ); + } // Ensure the arguments are valid. for (const arg of directive.args) {