Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect non-POJO objects in setValues #4289

Merged
merged 2 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/shared/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getTag, isPlainObject } from './utils';

describe('getTag()', () => {
test('should return [object Undefined] for undefined type', () => {
const tag = getTag(undefined);

expect(tag).toBe('[object Undefined]');
});

test('should return [object Null] for null type', () => {
const tag = getTag(null);

expect(tag).toBe('[object Null]');
});
});

describe('isPlainObject()', () => {
test('should return true if object has no prototype', () => {
const result = isPlainObject(Object.create(null));

expect(result).toBe(true);
});
});
28 changes: 27 additions & 1 deletion packages/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,35 @@ export function toNumber(value: string): number | string {
return isNaN(n) ? value : n;
}

export function isObjectLike(value: any) {
return typeof value === 'object' && value !== null;
}

export function getTag(value: any) {
if (value == null) {
return value === undefined ? '[object Undefined]' : '[object Null]';
}
return Object.prototype.toString.call(value);
}

// Reference: https://github.com/lodash/lodash/blob/master/isPlainObject.js
export function isPlainObject(value: any) {
if (!isObjectLike(value) || getTag(value) !== '[object Object]') {
return false;
}
if (Object.getPrototypeOf(value) === null) {
return true;
}
let proto = value;
while (Object.getPrototypeOf(proto) !== null) {
proto = Object.getPrototypeOf(proto);
}
return Object.getPrototypeOf(value) === proto;
}

export function merge(target: any, source: any) {
Object.keys(source).forEach(key => {
if (isObject(source[key])) {
if (isPlainObject(source[key])) {
if (!target[key]) {
target[key] = {};
}
Expand Down
25 changes: 25 additions & 0 deletions packages/vee-validate/tests/Form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,31 @@ describe('<Form />', () => {
expect(inputs[1].value).toBe(values.password);
});

test('sets non-plain object field with setValues()', async () => {
const wrapper = mountWithHoc({
template: `
<VForm ref="form">
<Field id="nonPlain" name="nonPlain" />
</VForm>
`,
});

await flushPromises();

class NonPlain {
field = 5;
}
const nonPlain = new NonPlain();
(wrapper.$refs as any)?.form.setValues({
nonPlain,
});

const realValues = (wrapper.$refs as any).form.getValues();
await flushPromises();
expect(realValues.nonPlain).toBeInstanceOf(NonPlain);
expect(realValues.nonPlain).toStrictEqual(nonPlain);
});

test('handles submit with handleSubmit and passing the event object', async () => {
const spy = vi.fn();
const wrapper = mountWithHoc({
Expand Down