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: handle nested value change validation #3926 #3929

Merged
merged 1 commit into from
Sep 18, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/vee-validate/src/useField.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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