Validation a request as function without middleware

32 Views Asked by At

is there a use of express-validator as non-middleware, just a function to send request json and take errors as a return.

import express from 'express';
import { body, validationResult } from 'express-validator';

function UserRegisterValidation() {
  return [
    body('username')
      .exists()
  ]
}

const app = express();
const port = 3000;

app.use(express.json());
app.post('/register', UserRegisterValidation(), (req, res) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    return res.status(400).json({ errors: errors.array() });
  }
  res.sendStatus(200);
});

app.listen(port, () => console.log('HTTP server started at port 3000'));

Expected Example


function UserRegisterValidation(requestJSON) {
let errors = requestJSON.filter(
[
    body('username')
      .exists()
  ]
)

  return errors;
}
0

There are 0 best solutions below