From da1f7a21ddc7b9aa3dd71e6bc0fae90cf7fbbdac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kiho=20=C2=B7=20Cham?= Date: Mon, 19 Aug 2019 21:15:28 +0800 Subject: [PATCH] fix: getSchema returning undefined doesn't break setIn (#269) --- packages/utils/src/__tests__/index.spec.js | 15 +++++++++++++++ packages/utils/src/accessor.ts | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/utils/src/__tests__/index.spec.js b/packages/utils/src/__tests__/index.spec.js index 6cb6e38f5b1..c0fa3d45fd3 100644 --- a/packages/utils/src/__tests__/index.spec.js +++ b/packages/utils/src/__tests__/index.spec.js @@ -31,6 +31,21 @@ test('test setIn auto create array', () => { ).toBeTruthy() }) +test('getSchema return undefined', () => { + const value = {} + setIn(value, 'array.0.bb.2', 'hello world', () => {}) + + expect( + isEqual(value, { + array: [ + { + bb: [undefined, undefined, 'hello world'] + } + ] + }) + ).toBeTruthy() +}) + test('test setIn dose not affect other items', () => { const value = { aa: [ diff --git a/packages/utils/src/accessor.ts b/packages/utils/src/accessor.ts index 134983d6752..bcf862d9cee 100644 --- a/packages/utils/src/accessor.ts +++ b/packages/utils/src/accessor.ts @@ -504,7 +504,8 @@ function _setIn( if (/^\d+$/.test(pathArr[i + 1 + ''])) { if (getSchema) { const schema = getSchema(pathArr.slice(0, i) as string[]) - if (schema.type === 'array') { + + if (!schema || schema.type === 'array') { obj[p] = [] } else { obj[p] = {}