Skip to content

Commit

Permalink
fix: initial validation not respecting the config opts
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed May 13, 2020
1 parent 486babd commit 2443d44
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions packages/core/src/useField.ts
Expand Up @@ -45,31 +45,31 @@ export function useField(
bails,
});

patch(result);
// Must be updated regardless if a mutation is needed or not
// FIXME: is this needed?
meta.valid.value = result.valid;
meta.invalid.value = !result.valid;

return result;
};

watch(value, runValidation, {
const runValidationWithMutation = () => runValidation().then(patch);

watch(value, runValidationWithMutation, {
deep: true,
});

if (isRef(rules)) {
watch(rules, runValidation, {
watch(rules, runValidationWithMutation, {
deep: true,
});
}

onMounted(() => {
validate(value.value, unwrap(rules)).then(result => {
runValidation().then(result => {
if (immediate) {
patch(result);
return;
}

// Initial silent validation.
meta.valid.value = result.valid;
meta.invalid.value = !result.valid;
});
});

Expand All @@ -78,15 +78,15 @@ export function useField(
});

const field = {
vid: fieldName,
vid: fieldName, // FIXME: is is needed anymore?
name: fieldName, // TODO: Custom field names
value: value,
meta,
errors,
errorMessage,
failedRules,
reset,
validate: runValidation,
validate: runValidationWithMutation,
handleChange,
onBlur,
__setRules(fn: GenericValidateFunction) {
Expand Down Expand Up @@ -145,6 +145,8 @@ function useValidationState(value: Ref<any>) {
meta.validated.value = true;
meta.pending.value = false;
failedRules.value = result.failedRules;

return result;
}

// Resets the validation state
Expand Down

0 comments on commit 2443d44

Please sign in to comment.