Skip to content

Commit

Permalink
fix: handle nested value change validation #3926 (#3929)
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Sep 18, 2022
1 parent cdaf22d commit 771e7f2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/vee-validate/src/useField.ts
Expand Up @@ -219,16 +219,18 @@ function _useField<TValue = unknown>(
}

let unwatchValue: WatchStopHandle;
let lastWatchedValue = deepCopy(value.value);
function watchValue() {
unwatchValue = watch(
value,
(val, oldVal) => {
if (isEqual(val, oldVal)) {
if (isEqual(val, oldVal) && isEqual(val, lastWatchedValue)) {
return;
}

const validateFn = validateOnValueUpdate ? validateWithStateMutation : validateValidStateOnly;
validateFn();
lastWatchedValue = deepCopy(val);
},
{
deep: true,
Expand Down
38 changes: 38 additions & 0 deletions packages/vee-validate/tests/useField.spec.ts
Expand Up @@ -29,6 +29,44 @@ describe('useField()', () => {
expect(error?.textContent).toBe(REQUIRED_MESSAGE);
});

// #3926
test('validates when nested value changes', async () => {
mountWithHoc({
setup() {
const { value, errorMessage } = useField<any>(
'field',
val => {
if (!val?.name) {
return REQUIRED_MESSAGE;
}

return true;
},
{
initialValue: { name: 'test' },
}
);

onMounted(() => {
value.value.name = '';
});

return {
value,
errorMessage,
};
},
template: `
<span>{{ errorMessage }}</span>
`,
});

const error = document.querySelector('span');

await flushPromises();
expect(error?.textContent).toBe(REQUIRED_MESSAGE);
});

test('valid flag is correct after reset', async () => {
mountWithHoc({
setup() {
Expand Down

0 comments on commit 771e7f2

Please sign in to comment.