Skip to content

Commit

Permalink
BREAKING: 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 c01d402
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/language/__tests__/parser-test.ts
Expand Up @@ -120,6 +120,13 @@ describe('Parser', () => {
});
});

it('does not allow "true" as enum value', () => {
expectSyntaxError('enum Test { VALID, true }').to.deep.equal({
message: 'Syntax Error: Name "true" is not a valid 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
2 changes: 1 addition & 1 deletion src/language/ast.ts
Expand Up @@ -584,7 +584,7 @@ export interface EnumValueDefinitionNode {
readonly kind: 'EnumValueDefinition';
readonly loc?: Location;
readonly description?: StringValueNode;
readonly name: NameNode;
readonly name: EnumValueNode;
readonly directives?: ReadonlyArray<ConstDirectiveNode>;
}

Expand Down
23 changes: 20 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.parseEnumValue();
const directives = this.parseConstDirectives();
return this.node<EnumValueDefinitionNode>(start, {
kind: Kind.ENUM_VALUE_DEFINITION,
Expand All @@ -1052,6 +1050,25 @@ export class Parser {
});
}

/**
* EnumValue : Name but not `true`, `false` or `null`
*/
parseEnumValue(): EnumValueNode {
const node = this.parseName();
if (node.value === 'true' || node.value === 'false' || node.value === 'null') {
throw syntaxError(
this._lexer.source,
this._lexer.lastToken.start,
`${getTokenDesc(this._lexer.lastToken)} is not a valid enum value.`,
);
}
return {
kind: 'EnumValue',
value: node.value,
loc: node.loc
};
}

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

0 comments on commit c01d402

Please sign in to comment.