Skip to content

Commit

Permalink
fix: disallow "true", "false", and "null" as enum values
Browse files Browse the repository at this point in the history
The specification disallows these in the SDL grammar definition, and using one of these enum values would result in a schema that would throw validation errors on creation.

Fixes #3221.
  • Loading branch information
Yogu committed Aug 4, 2021
1 parent 6a5f51f commit 4e44107
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/language/__tests__/parser-test.ts
Expand Up @@ -120,6 +120,21 @@ describe('Parser', () => {
});
});

it('does not allow "true", "false", or "null" as enum value', () => {
expectSyntaxError('enum Test { VALID, true }').to.deep.equal({
message: 'Syntax Error: Name "true" is reserved and cannot be used for an enum value.',
locations: [{ line: 1, column: 20 }],
});
expectSyntaxError('enum Test { VALID, false }').to.deep.equal({
message: 'Syntax Error: Name "false" is reserved and cannot be used for an enum value.',
locations: [{ line: 1, column: 20 }],
});
expectSyntaxError('enum Test { VALID, null }').to.deep.equal({
message: 'Syntax Error: Name "null" is reserved and cannot be used for an enum value.',
locations: [{ line: 1, column: 20 }],
});
});

it('parses multi-byte characters', () => {
// Note: \u0A0A could be naively interpreted as two line-feed chars.
const ast = parse(`
Expand Down
21 changes: 18 additions & 3 deletions src/language/parser.ts
Expand Up @@ -1036,13 +1036,11 @@ export class Parser {

/**
* EnumValueDefinition : Description? EnumValue Directives[Const]?
*
* EnumValue : Name
*/
parseEnumValueDefinition(): EnumValueDefinitionNode {
const start = this._lexer.token;
const description = this.parseDescription();
const name = this.parseName();
const name = this.parseEnumValueName();
const directives = this.parseConstDirectives();
return this.node<EnumValueDefinitionNode>(start, {
kind: Kind.ENUM_VALUE_DEFINITION,
Expand All @@ -1052,6 +1050,23 @@ export class Parser {
});
}

/**
* EnumValue : Name but not `true`, `false` or `null`
*/
parseEnumValueName(): NameNode {
if (this._lexer.token.value === 'true' ||
this._lexer.token.value === 'false' ||
this._lexer.token.value === 'null'
) {
throw syntaxError(
this._lexer.source,
this._lexer.token.start,
`${getTokenDesc(this._lexer.token)} is reserved and cannot be used for an enum value.`,
);
}
return this.parseName();
}

/**
* InputObjectTypeDefinition :
* - Description? input Name Directives[Const]? InputFieldsDefinition?
Expand Down

0 comments on commit 4e44107

Please sign in to comment.