Skip to content

Commit 670bbac

Browse files
coerceValues: correctly handle NaN and similar values (#2047)
1 parent bbab902 commit 670bbac

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/utilities/__tests__/coerceValue-test.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ describe('coerceValue', () => {
7171
expectValue(result).to.equal(null);
7272
});
7373

74+
it('returns no error for NaN result', () => {
75+
const result = coerceValue({ value: NaN }, TestScalar);
76+
expectValue(result).to.satisfy(Number.isNaN);
77+
});
78+
7479
it('returns an error for undefined result', () => {
7580
const result = coerceValue({ value: undefined }, TestScalar);
7681
expectErrors(result).to.deep.equal(['Expected type TestScalar.']);
@@ -140,9 +145,9 @@ describe('coerceValue', () => {
140145
});
141146

142147
it('returns an error for an invalid field', () => {
143-
const result = coerceValue({ foo: 'abc' }, TestInputObject);
148+
const result = coerceValue({ foo: NaN }, TestInputObject);
144149
expectErrors(result).to.deep.equal([
145-
'Expected type Int at value.foo. Int cannot represent non-integer value: "abc"',
150+
'Expected type Int at value.foo. Int cannot represent non-integer value: NaN',
146151
]);
147152
});
148153

@@ -184,7 +189,10 @@ describe('coerceValue', () => {
184189
new GraphQLInputObjectType({
185190
name: 'TestInputObject',
186191
fields: {
187-
foo: { type: GraphQLInt, defaultValue },
192+
foo: {
193+
type: new GraphQLScalarType({ name: 'TestScalar' }),
194+
defaultValue,
195+
},
188196
},
189197
});
190198

@@ -199,8 +207,15 @@ describe('coerceValue', () => {
199207
});
200208

201209
it('returns null as value', () => {
202-
const result = coerceValue({}, TestInputObject(7));
203-
expectValue(result).to.deep.equal({ foo: 7 });
210+
const result = coerceValue({}, TestInputObject(null));
211+
expectValue(result).to.deep.equal({ foo: null });
212+
});
213+
214+
it('returns NaN as value', () => {
215+
const result = coerceValue({}, TestInputObject(NaN));
216+
expectValue(result)
217+
.to.have.property('foo')
218+
.that.satisfy(Number.isNaN);
204219
});
205220
});
206221

src/utilities/coerceValue.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import { forEach, isCollection } from 'iterall';
44
import objectValues from '../polyfills/objectValues';
55
import inspect from '../jsutils/inspect';
6-
import isInvalid from '../jsutils/isInvalid';
76
import didYouMean from '../jsutils/didYouMean';
87
import isObjectLike from '../jsutils/isObjectLike';
98
import suggestionList from '../jsutils/suggestionList';
@@ -63,7 +62,7 @@ export function coerceValue(
6362
// the original error.
6463
try {
6564
const parseResult = type.parseValue(value);
66-
if (isInvalid(parseResult)) {
65+
if (parseResult === undefined) {
6766
return ofErrors([
6867
coercionError(`Expected type ${type.name}`, blameNode, path),
6968
]);
@@ -145,8 +144,8 @@ export function coerceValue(
145144
// Ensure every defined field is valid.
146145
for (const field of objectValues(fields)) {
147146
const fieldValue = value[field.name];
148-
if (isInvalid(fieldValue)) {
149-
if (!isInvalid(field.defaultValue)) {
147+
if (fieldValue === undefined) {
148+
if (field.defaultValue !== undefined) {
150149
coercedValue[field.name] = field.defaultValue;
151150
} else if (isNonNullType(field.type)) {
152151
errors = add(

0 commit comments

Comments
 (0)