Skip to content

Commit 63ab42a

Browse files
committed
Ignore undefined one-of fields in runtime maps
1 parent 147c7ea commit 63ab42a

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

src/utilities/__tests__/coerceInputValue-test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ describe('coerceInputValue', () => {
203203
test({ bar: null }, TestInputObject, undefined);
204204
});
205205

206+
it('returns for valid input with an additional undefined field', () => {
207+
test({ foo: 123, bar: undefined }, TestInputObject, { foo: 123 });
208+
});
209+
206210
it('invalid for an invalid field', () => {
207211
test({ foo: NaN }, TestInputObject, undefined);
208212
});

src/utilities/coerceInputValue.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,15 @@ export function coerceInputValue(
7373

7474
const coercedValue: ObjMap<unknown> = Object.create(null);
7575
const fieldDefs = type.getFields();
76-
const hasUndefinedField = Object.keys(inputValue).some(
77-
(name) =>
78-
inputValue[name] !== undefined && !Object.hasOwn(fieldDefs, name),
79-
);
80-
if (hasUndefinedField) {
81-
return; // Invalid: intentionally return no value.
76+
let definedFieldCount = 0;
77+
for (const fieldName of Object.keys(inputValue)) {
78+
if (inputValue[fieldName] === undefined) {
79+
continue;
80+
}
81+
definedFieldCount++;
82+
if (!Object.hasOwn(fieldDefs, fieldName)) {
83+
return; // Invalid: intentionally return no value.
84+
}
8285
}
8386
for (const field of Object.values(fieldDefs)) {
8487
const fieldValue = inputValue[field.name];
@@ -101,7 +104,7 @@ export function coerceInputValue(
101104

102105
if (type.isOneOf) {
103106
const keys = Object.keys(coercedValue);
104-
if (keys.length !== 1) {
107+
if (definedFieldCount !== 1 || keys.length !== 1) {
105108
return; // Invalid: intentionally return no value.
106109
}
107110

0 commit comments

Comments
 (0)