Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to stop the validation flow to different checks #1262

Closed
Romulo-Moraes opened this issue Nov 9, 2023 · 1 comment
Closed

How to stop the validation flow to different checks #1262

Romulo-Moraes opened this issue Nov 9, 2023 · 1 comment

Comments

@Romulo-Moraes
Copy link

Is there any way to stop all validations when a check fails ?

Take a look at this example:

login(){
        // Messages in PT-BR, nothing important...
        return [
            cookie('token').custom((theToken) => !this.#verifyUserToken(theToken)).bail().withMessage('Não é preciso se autenticar pois o usuário já está autenticado'),
            body('email').isString().withMessage('O e-mail precisa ser uma string')
                         .notEmpty().withMessage('O e-mail não pode ser vazio')
                         .isEmail().withMessage('É necessário um e-mail válido'),
            body('password').isString().withMessage('A senha precisa ser do tipo string')
                            .notEmpty().withMessage('A senha é um campo necessário para o login')
        ];
    }

I know that the .bail() can stop the cascade validation, but looks like that it only stops for the same check.
The problem is that is really unnecessary report any email or password fail if the user is already logged.

The login is the least complicated, the register validations is also using external database queries, and if the user is already logged, there's no sense of doing everything for just report that the token is valid and a lot of another useless stuff.

I searched the repository's issues and i found a similar question #696, such question that was marked as duplicated of a issue that discuss about the .bail() and no useful information about this kind of problem.

My final question is: Is there any way for stop all the next validations on a validation fail? if not, is there a different approach that i a can get the same expected results?

Thank you for the help!

@Romulo-Moraes
Copy link
Author

.bail( { level : 'request' } )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant