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 committed Jun 21, 2024
1 parent b78377b commit 36e59f4
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 @@ -431,6 +431,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: Root types must all be different if provided', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/type/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,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 36e59f4

Please sign in to comment.