Skip to content

Commit 2a09a58

Browse files
committed
fix: check if the target is pojo before attempted merge (#4615)
1 parent 61e75a3 commit 2a09a58

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

.changeset/fluffy-icons-eat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"vee-validate": patch
3+
---
4+
5+
"fix: check if both source and target objects are POJOs"

packages/shared/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function isPlainObject(value: any) {
5151

5252
export function merge(target: any, source: any) {
5353
Object.keys(source).forEach(key => {
54-
if (isPlainObject(source[key])) {
54+
if (isPlainObject(source[key]) && isPlainObject(target[key])) {
5555
if (!target[key]) {
5656
target[key] = {};
5757
}

packages/vee-validate/tests/useForm.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,4 +1398,31 @@ describe('useForm()', () => {
13981398
await flushPromises();
13991399
await expect(form.errors.value.fname).toBe(undefined);
14001400
});
1401+
1402+
test('checks if both source and target are POJO before setting properties', async () => {
1403+
let form!: FormContext<{ file: { name: string; size: number } }>;
1404+
const f1 = new File([''], 'f1.text');
1405+
const f2 = { name: 'f2.text', size: 123 };
1406+
1407+
mountWithHoc({
1408+
setup() {
1409+
form = useForm({
1410+
initialValues: { file: f1 },
1411+
});
1412+
1413+
form.defineField('file');
1414+
1415+
return {};
1416+
},
1417+
template: `
1418+
<div></div>
1419+
`,
1420+
});
1421+
1422+
await flushPromises();
1423+
expect(form.values.file).toBeInstanceOf(File);
1424+
expect(form.values.file).toBe(f1);
1425+
form.setValues({ file: f2 });
1426+
expect(form.values.file).toEqual(f2);
1427+
});
14011428
});

0 commit comments

Comments
 (0)