Skip to content

Forbid 'true', 'false' and 'null' as names for Enum value #812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions src/type/__tests__/validation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,43 @@ describe('Type System: Enum types must be well defined', () => {
);
});

it('rejects an Enum type with incorrectly named values', () => {
function enumValue(name) {
return new GraphQLEnumType({
name: 'SomeEnum',
values: {
[name]: {}
}
});
}

expect(() => enumValue('#value')
).to.throw('Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "#value" does not.');

expect(() => enumValue('true')
).to.throw('Name "true" is can not be used for Enum value.');

expect(() => enumValue('false')
).to.throw('Name "false" is can not be used for Enum value.');

expect(() => enumValue('null')
).to.throw('Name "null" is can not be used for Enum value.');
});

it('does not allow isDeprecated without deprecationReason on enum', () => {
expect(() =>
new GraphQLEnumType({
name: 'SomeEnum',
values: {
value: { isDeprecated: true }
}
})
).to.throw(
'SomeEnum.value should provide "deprecationReason" instead ' +
'of "isDeprecated".'
);
});

});


Expand Down Expand Up @@ -2065,18 +2102,4 @@ describe('Objects must adhere to Interface they implement', () => {
);
});

it('does not allow isDeprecated without deprecationReason on enum', () => {
expect(() =>
new GraphQLEnumType({
name: 'SomeEnum',
values: {
value: { isDeprecated: true }
}
})
).to.throw(
'SomeEnum.value should provide "deprecationReason" instead ' +
'of "isDeprecated".'
);
});

});
5 changes: 5 additions & 0 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,11 @@ function defineEnumValues(
);
return valueNames.map(valueName => {
assertValidName(valueName);
invariant(
[ 'true', 'false', 'null' ].indexOf(valueName) === -1,
`Name "${valueName}" is can not be used for Enum value.`
);

const value = valueMap[valueName];
invariant(
isPlainObj(value),
Expand Down