mikeerickson / validatorjs

A data validation library in JavaScript for the browser and Node.js, inspired by Laravel's Validator.
https://www.npmjs.com/package/validatorjs
MIT License
1.77k stars 280 forks source link

Stop validation process if a rule return false, in checkAsync #425

Open iarkaroy opened 3 years ago

iarkaroy commented 3 years ago

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 commented 3 years ago

Any update on this?

mikeerickson commented 3 years ago

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