diff --git a/src/utils/setByPath.test.ts b/src/utils/setByPath.test.ts index 99f6ad35..0332cc14 100644 --- a/src/utils/setByPath.test.ts +++ b/src/utils/setByPath.test.ts @@ -356,4 +356,21 @@ describe('setByPath', () => { numbers: [98618922, -367] }) }) + + it('should replace an existing object with a primitive value', () => { + const objUnderTest = { + event: { + startDateTime: { + format: 'date-time' + } + } + } + + const result = setByPath(objUnderTest, 'event.startDateTime', '2002-12-01') + expect(result).toEqual({ + event: { + startDateTime: '2002-12-01' + } + }) + }) }) diff --git a/src/utils/setByPath.ts b/src/utils/setByPath.ts index 25ee7a22..4f3a2ec6 100644 --- a/src/utils/setByPath.ts +++ b/src/utils/setByPath.ts @@ -23,6 +23,13 @@ export const setByPath = ( dot.keepArray = true } + // Check if the path leads to an object that should be replaced + const currentValue = dot.pick(path, objectOrArray) + if (isObject(currentValue) && !isObject(newValue)) { + // Directly replace the object with the new value + dot.del(path, objectOrArray) + } + // Handle array of objects if (Array.isArray(objectOrArray)) { // Handle array definition (example: [0])