Skip to content

Commit 8715819

Browse files
author
Robert Zhu
committed
Throw when parsing empty string to numeric scalar
1 parent 3142d87 commit 8715819

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/type/__tests__/serialization-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ describe('Type System: Scalar coercion', () => {
8181
expect(
8282
GraphQLInt.serialize(true)
8383
).to.equal(1);
84+
expect(() =>
85+
GraphQLInt.serialize('')
86+
).to.throw(
87+
'Int cannot represent non 32-bit signed integer value: (empty string)'
88+
);
8489
});
8590

8691
it('serializes output float', () => {
@@ -126,6 +131,12 @@ describe('Type System: Scalar coercion', () => {
126131
).to.throw(
127132
'Float cannot represent non numeric value: one'
128133
);
134+
135+
expect(() =>
136+
GraphQLFloat.serialize('')
137+
).to.throw(
138+
'Float cannot represent non numeric value: (empty string)'
139+
);
129140
});
130141

131142
it('serializes output strings', () => {

src/type/scalars.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ const MAX_INT = 2147483647;
2020
const MIN_INT = -2147483648;
2121

2222
function coerceInt(value: mixed): ?number {
23+
if (value === '') {
24+
throw new TypeError(
25+
'Int cannot represent non 32-bit signed integer value: (empty string)'
26+
);
27+
}
2328
const num = Number(value);
2429
if (num === num && num <= MAX_INT && num >= MIN_INT) {
2530
return (num < 0 ? Math.ceil : Math.floor)(num);
@@ -48,6 +53,11 @@ export const GraphQLInt = new GraphQLScalarType({
4853
});
4954

5055
function coerceFloat(value: mixed): ?number {
56+
if (value === '') {
57+
throw new TypeError(
58+
'Float cannot represent non numeric value: (empty string)'
59+
);
60+
}
5161
const num = Number(value);
5262
if (num === num) {
5363
return num;

0 commit comments

Comments
 (0)