Skip to content

Commit

Permalink
fix: avoid validating dependencies via watcheffect closes #3156
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Feb 6, 2021
1 parent 52fdb8a commit a7b91f6
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions packages/vee-validate/src/useField.ts
Expand Up @@ -6,7 +6,6 @@ import {
reactive,
computed,
onMounted,
watchEffect,
onBeforeUnmount,
unref,
WatchStopHandle,
Expand Down Expand Up @@ -215,28 +214,38 @@ export function useField<TValue = any>(
}

return Object.keys(rulesVal).reduce((acc: string[], rule: string) => {
const deps = extractLocators((normalizedRules as Ref<Record<string, any>>).value[rule]).map(
(dep: any) => dep.__locatorRef
);
const deps = extractLocators(rulesVal[rule]).map((dep: any) => dep.__locatorRef);

acc.push(...deps);

return acc;
}, []);
});

const dependenciesValues = computed(() => {
return dependencies.value.reduce((acc, depName) => {
if (depName in form.values) {
acc[depName] = form.values[depName];
}

return acc;
}, {} as Record<string, unknown>);
});

// Adds a watcher that runs the validation whenever field dependencies change
watchEffect(() => {
// Skip if no dependencies
if (!dependencies.value.length) {
watch(dependenciesValues, (deps, oldDeps) => {
// Skip if no dependencies or if the field wasn't manipulated
if (!Object.keys(deps).length || !meta.dirty) {
return;
}

// For each dependent field, validate it if it was validated before
dependencies.value.forEach(dep => {
if (dep in form.values && meta.dirty) {
return validate();
}
const shouldValidate = Object.keys(deps).some(depName => {
return deps[depName] !== oldDeps[depName];
});

if (shouldValidate) {
validate();
}
});

return field;
Expand Down

0 comments on commit a7b91f6

Please sign in to comment.