question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Asynchronous Validators on the Next branch

See original GitHub issue

Good time of the day.
I’ve decided to play around with the next branch (or @vuelidate/core and @vuelidate/validators) and i’ve faced an issue i cannot wrap my head around.

Right now, on the stable branch i am using the following validator to perform async validations:

export function asynchronousValidator (validator: Function, delay: number = 2000) {
    let currentTimer: NodeJS.Timeout|null = null;
    let currentPromiseReject: any = null;

    const debounce = () : Promise<void> => new Promise((resolve, reject) => {
        currentTimer = setTimeout(() => {
            currentTimer = null;
            currentPromiseReject = null;
            resolve();
        }, delay);
        currentPromiseReject = reject;
    });

    return (value: any) => {
        if (currentTimer) {
            currentPromiseReject(new Error('Replaced'));
            clearTimeout(currentTimer);
            currentTimer = null;
        }
        // @ts-ignore
        return validator.call(this, value, debounce);
    }
}

and i have this validation in place:

username: {
    required,
    minLength: minLength(8),
    maxLength: maxLength(32),
    isUnique: asynchronousValidator(function (value: string, debounce: Function) {
        if (isUndefinedOrNull(value) || value === '') {
            return true;
        }
        return debounce()
            .then(() => PeopleService.usernameExists(value))
            .then((exists: boolean) => !exists)
            .catch(() => false);
    }, 1200)
}

However, on the next branch, there is a new way of creating asynchronous validators. By going through the source code, i found that you can do the following common.withAsync((value: string) => {});

However, for some reason, there is no way in adapting the code i have (for input debouncing). Either it is not working at all, it gets called (by using the console.log i can see that each part of it is working), but there is no output (validation not happens). Or, it constantly throwing errors about not being able to access the .then method as it does not exists.

I’ve tried this

export function asynchronousValidator (validator: Function, delay: number = 2000) {
    let currentTimer: NodeJS.Timeout|null = null;
    let currentPromiseReject: any = null;

    const debounce = () : Promise<void> => new Promise((resolve, reject) => {
        currentTimer = setTimeout(() => {
            currentTimer = null;
            currentPromiseReject = null;
            resolve();
        }, delay);
        currentPromiseReject = reject;
    });

    return withAsync((value: any) => {
        if (currentTimer) {
            currentPromiseReject(new Error('Replaced'));
            clearTimeout(currentTimer);
            currentTimer = null;
        }
        // @ts-ignore
        return validator.call(this, value, debounce);
    });
}

But as i said, no result is returned, or this error is thrown (yes, can be both, no idea why). image

I am using latest Vue 2 with latest composition-api on latest nuxtjs

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
outerstormcommented, Nov 13, 2020

Ran into this same issue and looked at your code sandbox (https://codesandbox.io/s/vuelidate-async-validator-forked-uxtgm), but changed the promise to resolve true (rather than false) and the field still shows as invalid?

1reaction
dobromir-hristovcommented, Nov 4, 2020
rule: withAsync(funcReturningPromise)
Read more comments on GitHub >

github_iconTop Results From Across the Web

All About Validators in Angular + Creating Custom Sync ...
Validators in angular are just simple functions that check our form values and return an error if some things are not the way...
Read more >
How to do asynchronous validator in Angular 7? - Medium
Validator is a function which returns Observable object when some error occurs or null value when validation passed positive. This behaviour is ...
Read more >
How to add Async Validation to Angular Reactive Forms
Let's first, add the synchronous validators – we'll add Validators.required (meaning the field can't be empty) and Validators.email (meaning the ...
Read more >
Async validator - Data driven forms
You can use a Async function as a validator. But it must be first in the validate array. Other async validators will be...
Read more >
Control with async validator changes its state to pending when ...
Next, the call to this._runAsyncValidator(opts.emitEvent) will be called. This will trigger the async validator to run again with an ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found