2

I want to bail before it reaches my custom validators.

   public classification = () => [
        param("id").exists({ checkNull: true }).isMongoId().bail(),
        param("id")
            .custom(checkIfSessionExists)
            .custom(checkIfSessionIsCompletedOrFailed)
            .custom(checkIfSessionHasExpired)
            .custom(checkIfSessionContainsFrontImage),
        validationResultHandler,
    ]

Why can i not apply .bail() here: param("id").exists({ checkNull: true }).isMongoId().bail(),

Kay
  • 17,906
  • 63
  • 162
  • 270

2 Answers2

3

According to the express-validator documentation:

.bail() is useful to prevent a custom validator that touches a database or external API from running when you know it will fail. Can be used multiple times IN THE SAME validation chain if needed.

In your case it would be like:

[
  check('id')
   .exists({ checkNull: true }).bail()
   .isMongoId().bail()
   .custom(checkIfSessionExists).bail()
   .custom(checkIfSessionIsCompletedOrFailed).bail()
   .custom(checkIfSessionHasExpired).bail()
   .custom(checkIfSessionContainsFrontImage)
  ...
  ...
];

so, if some of this validation fail, the following validators will never run

-2

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
  },
);
  • This answer looks like ChatGPT – DavidW Jul 15 '23 at 08:54
  • And then it was changed in revision 2 to something completely different. Now it has several spelling mistakes, a missing word, etc. Now it *looks* like [close paraphrasing](https://en.wikipedia.org/wiki/Wikipedia:Close_paraphrasing) of the other answer (or perhaps a common source). For instance, the first sentence. Or perhaps not so close paraphrasing. What is going on here? – Peter Mortensen Jul 17 '23 at 09:42
  • cont' - [One of](https://stackoverflow.com/questions/40880416/what-is-the-difference-between-event-loop-queue-and-job-queue/68578901#68578901) the other visible answers (from before [December 2022](https://meta.stackoverflow.com/questions/421831/temporary-policy-chatgpt-is-banned)) is completely devoid of punctuation (thus practically ***incomprehensible***) and [the other](https://stackoverflow.com/questions/11739715/how-to-redirect-in-node-js/73011250#73011250) is very terse. – Peter Mortensen Jul 17 '23 at 09:47