validatorjs icon indicating copy to clipboard operation
validatorjs copied to clipboard

Stop validation process if a rule return false, in checkAsync

Open iarkaroy opened this issue 3 years ago • 2 comments

I do not know if this is doable currently. I could not find any doc for this. I want the validator to stop immediately when a rule for an attribute fails. It seems the validator continues to execute all the rules for that attribute. This is happening only when using checkAsync.

Example:

const Validator = require('validatorjs');

Validator.registerAsync(
    'unique',
    (value, attribute, req, passes) => {
        console.log('unique called');
        // Pseudo code
        setTimeout(() => {
            passes(false, 'The username already exists');
        }, 500);
    },
    'The :attribute does not exist'
);

let validator = new Validator({username: 1234}, { username: 'required|string|unique' });
validator.checkAsync(
    () => {
        console.log('passes');
    },
    () => {
        console.log('fails');
    }
);

In above code, the unique rule should not be executed (at least, in my use case). But all the rules are being executed and unique called is printed on console.

How can I stop immediately when a rule fails?

iarkaroy avatar May 02 '21 09:05 iarkaroy

Any update on this?

iarkaroy avatar Jun 07 '21 18:06 iarkaroy

I have not had a chance to review this deeper, but for clarification sake, I am understanding you want this validation to fail at the string rule and stop all remaining validation for the attribute under test, this never hitting unique , but all remaining futS will continue

mikeerickson avatar Jun 07 '21 19:06 mikeerickson