Skip to content

Commit

Permalink
Require non-empty directive locations (#4100)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbellenger authored and benjie committed Jul 1, 2024
1 parent e1ceb8f commit 6f459d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/type/__tests__/validation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/type/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6f459d0

Please sign in to comment.