From 4d6cc035e21953906ddcf2abfad0ce0f36d494e0 Mon Sep 17 00:00:00 2001 From: Jonathan Ziller Date: Fri, 11 Jan 2019 10:05:49 +0100 Subject: [PATCH] fix: allow boxed `undefined` values as form control values --- src/state.spec.ts | 8 ++++++++ src/state.ts | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/src/state.spec.ts b/src/state.spec.ts index 8712d347..41a3898a 100644 --- a/src/state.spec.ts +++ b/src/state.spec.ts @@ -426,9 +426,13 @@ describe('state', () => { const stringValue = 'A'; const numberValue = 101; const booleanValue = true; + const nullValue = null; + const undefinedValue = undefined; expect(verifyFormControlValueIsValid(stringValue)).toBe(stringValue); expect(verifyFormControlValueIsValid(numberValue)).toBe(numberValue); expect(verifyFormControlValueIsValid(booleanValue)).toBe(booleanValue); + expect(verifyFormControlValueIsValid(nullValue)).toBe(nullValue); + expect(verifyFormControlValueIsValid(undefinedValue)).toBe(undefinedValue); }); it('should throw for invalid values', () => { @@ -446,11 +450,15 @@ describe('state', () => { const boxedBooleanValue = box(true); const boxedObjectValue = box({ v: 'A' }); const boxedArrayValue = box(['A']); + const boxedNullValue = box(null); + const boxedUndefinedValue = box(undefined); expect(verifyFormControlValueIsValid(boxedStringValue)).toBe(boxedStringValue); expect(verifyFormControlValueIsValid(boxedNumberValue)).toBe(boxedNumberValue); expect(verifyFormControlValueIsValid(boxedBooleanValue)).toBe(boxedBooleanValue); expect(verifyFormControlValueIsValid(boxedObjectValue)).toBe(boxedObjectValue); expect(verifyFormControlValueIsValid(boxedArrayValue)).toBe(boxedArrayValue); + expect(verifyFormControlValueIsValid(boxedNullValue)).toBe(boxedNullValue); + expect(verifyFormControlValueIsValid(boxedUndefinedValue)).toBe(boxedUndefinedValue); }); it('should throw for non-serializable boxed values', () => { diff --git a/src/state.ts b/src/state.ts index 6be3eedd..165d3e9e 100644 --- a/src/state.ts +++ b/src/state.ts @@ -603,6 +603,10 @@ export function verifyFormControlValueIsValid(value: TValue) { throw new Error(`${errorMsg}; got ${JSON.stringify(value)} of type ${typeof value}`); // `; } + if (value.value === null || ['string', 'number', 'boolean', 'undefined'].indexOf(typeof value.value) >= 0) { + return value; + } + const serialized = JSON.stringify(value); const deserialized = JSON.parse(serialized);