Skip to content

Commit

Permalink
fix: allow boxed undefined values as form control values
Browse files Browse the repository at this point in the history
  • Loading branch information
MrWolfZ committed Jan 11, 2019
1 parent ce8696d commit 4d6cc03
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down
4 changes: 4 additions & 0 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,10 @@ export function verifyFormControlValueIsValid<TValue>(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);

Expand Down

0 comments on commit 4d6cc03

Please sign in to comment.