Skip to content

Commit

Permalink
fix: check if the target is pojo before attempted merge (#4615)
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Dec 27, 2023
1 parent 61e75a3 commit 2a09a58
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fluffy-icons-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vee-validate": patch
---

"fix: check if both source and target objects are POJOs"
2 changes: 1 addition & 1 deletion packages/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function isPlainObject(value: any) {

export function merge(target: any, source: any) {
Object.keys(source).forEach(key => {
if (isPlainObject(source[key])) {
if (isPlainObject(source[key]) && isPlainObject(target[key])) {
if (!target[key]) {
target[key] = {};
}
Expand Down
27 changes: 27 additions & 0 deletions packages/vee-validate/tests/useForm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1398,4 +1398,31 @@ describe('useForm()', () => {
await flushPromises();
await expect(form.errors.value.fname).toBe(undefined);
});

test('checks if both source and target are POJO before setting properties', async () => {
let form!: FormContext<{ file: { name: string; size: number } }>;
const f1 = new File([''], 'f1.text');
const f2 = { name: 'f2.text', size: 123 };

mountWithHoc({
setup() {
form = useForm({
initialValues: { file: f1 },
});

form.defineField('file');

return {};
},
template: `
<div></div>
`,
});

await flushPromises();
expect(form.values.file).toBeInstanceOf(File);
expect(form.values.file).toBe(f1);
form.setValues({ file: f2 });
expect(form.values.file).toEqual(f2);
});
});

0 comments on commit 2a09a58

Please sign in to comment.