diff --git a/docs/guides/manually-running.md b/docs/guides/manually-running.md index 288d3933..dc2d2668 100644 --- a/docs/guides/manually-running.md +++ b/docs/guides/manually-running.md @@ -25,22 +25,19 @@ Check the examples below to understand how this method can help you: ```js const express = require('express'); const { validationResult } = require('express-validator'); -// can be reused by many routes -// sequential processing, stops running validations chain if the previous one fails. +// can be reused by many routes const validate = validations => { return async (req, res, next) => { + // sequential processing, stops running validations chain if one fails. for (const validation of validations) { const result = await validation.run(req); - if (result.errors.length) break; - } - - const errors = validationResult(req); - if (errors.isEmpty()) { - return next(); + if (result.isEmpty()) { + return res.status(400).json({ errors: result.array() }); + } } - res.status(400).json({ errors: errors.array() }); + next(); }; }; @@ -59,22 +56,19 @@ app.post('/signup', validate([ ```typescript import express from 'express'; import { body, validationResult, ContextRunner } from 'express-validator'; -// can be reused by many routes -// sequential processing, stops running validations chain if the previous one fails. +// can be reused by many routes const validate = (validations: ContextRunner[]) => { return async (req: express.Request, res: express.Response, next: express.NextFunction) => { + // sequential processing, stops running validations chain if one fails. for (const validation of validations) { const result = await validation.run(req); - if (!result.isEmpty()) break; - } - - const errors = validationResult(req); - if (errors.isEmpty()) { - return next(); + if (!result.isEmpty()) { + return res.status(400).json({ errors: result.array() }); + } } - res.status(400).json({ errors: errors.array() }); + next(); }; }; diff --git a/website/versioned_docs/version-7.0.0/guides/manually-running.md b/website/versioned_docs/version-7.0.0/guides/manually-running.md index 9eae7de4..dc2d2668 100644 --- a/website/versioned_docs/version-7.0.0/guides/manually-running.md +++ b/website/versioned_docs/version-7.0.0/guides/manually-running.md @@ -25,22 +25,19 @@ Check the examples below to understand how this method can help you: ```js const express = require('express'); const { validationResult } = require('express-validator'); -// can be reused by many routes -// sequential processing, stops running validations chain if the previous one fails. +// can be reused by many routes const validate = validations => { return async (req, res, next) => { - for (let validation of validations) { + // sequential processing, stops running validations chain if one fails. + for (const validation of validations) { const result = await validation.run(req); - if (result.errors.length) break; - } - - const errors = validationResult(req); - if (errors.isEmpty()) { - return next(); + if (result.isEmpty()) { + return res.status(400).json({ errors: result.array() }); + } } - res.status(400).json({ errors: errors.array() }); + next(); }; }; @@ -59,22 +56,19 @@ app.post('/signup', validate([ ```typescript import express from 'express'; import { body, validationResult, ContextRunner } from 'express-validator'; -// can be reused by many routes -// sequential processing, stops running validations chain if the previous one fails. +// can be reused by many routes const validate = (validations: ContextRunner[]) => { return async (req: express.Request, res: express.Response, next: express.NextFunction) => { - for (let validation of validations) { + // sequential processing, stops running validations chain if one fails. + for (const validation of validations) { const result = await validation.run(req); - if (result.errors.length) break; - } - - const errors = validationResult(req); - if (errors.isEmpty()) { - return next(); + if (!result.isEmpty()) { + return res.status(400).json({ errors: result.array() }); + } } - res.status(400).json({ errors: errors.array() }); + next(); }; };