According to express validator docs, Stops running the validation chain if any of the previous validators failed.
This is useful to prevent a custom validator that touches a database or external API from running when you know it will fail.
This is also usefull when you want to stop validating when one of the validation failed in a chain.
.bail() can be used multiple times in the same validation chain if desired:
bail(options?: { level: 'chain' | 'request' }): ValidationChain
options.level | The level at which the validation chain should be stopped. Defaults to chain.
.isEmail()
// If not an email, stop here
.bail()
.custom(checkDenylistDomain)
// If domain is not allowed, don't go check if it already exists
.bail()
.custom(checkEmailExists);
If the level option is set to request, then also no further validation chains will run on the current request. For example:
app.get(
'/search',
query('query').notEmpty().bail({ level: 'request' }),
// If `query` is empty, then the following validation chains won't run:
query('query_type').isIn(['user', 'posts']),
query('num_results').isInt(),
(req, res) => {
// Handle request
},
);