Skip to content

Commit

Permalink
docs: fix "manually running" guide for v7+
Browse files Browse the repository at this point in the history
Fixes #1241
  • Loading branch information
gustavohenke committed May 18, 2024
1 parent 2c80f85 commit b182989
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 38 deletions.
30 changes: 12 additions & 18 deletions docs/guides/manually-running.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
};

Expand All @@ -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();
};
};

Expand Down
34 changes: 14 additions & 20 deletions website/versioned_docs/version-7.0.0/guides/manually-running.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
};

Expand All @@ -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();
};
};

Expand Down

0 comments on commit b182989

Please sign in to comment.