question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Get subset of request data filtered by validation rules

See original GitHub issue

Hello!

Thank you for this great module. However, I’m looking for a way to get a subset of request data filtered by validation rules.

I will give a small example:


/** Imagine that this is the request body.
{
  firstName: 'Slava',
  lastName: 'Fomin',
  email: 's.fomin@betsol.ru',
  password: 'some-pass-here',
  someExtra: 'foo'
}
*/

// I'm using this map to specify validation rules for the request.
req.checkBody({
  firstName: {},
  lastName: {},
  email: {
    notEmpty: {
      errorMessage: 'Missing E-Mail'
    },
    isEmail: {
      errorMessage: 'Incorrect E-Mail address'
    }
  }
});

var errors = req.validationErrors(true);
if (errors) {
  return res.json(400, {
    errors: errors
  });
}

var requestParams = req.getValidatedBody();

// We are getting only parameters specified during the validation phase,
// the extra request parameters, like "password", are ignored.
console.log(requestParams);

/**
{
  firstName: 'Slava',
  lastName: 'Fomin',
  email: 's.fomin@betsol.ru'
}
*/

That way I will be able to use the entire object without the fear of overwriting some protected property later on (e.g. when updating user entity).

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
gustavohenkecommented, Aug 27, 2017

Hello folks! v4.0.0 has finally been released!

You can now get the subset of data matched by any validations by using the new APIs check and filter:

const { check, validationResult } = require('express-validator/check');
const { matchedData } = require('express-validator/filter');

app.post('/', [
  check('username').isLength({ min: 1 }).isEmail(),
  check('password').isLength({ min: 1 })
], (req, res, next) => {
  // data will only contain valid validated data by default
  const data = matchedData(req);

  // you can also get all validated data, even the invalid ones:
  const data = matchedData(req, { onlyValidData: false });
});

Please check the README for complete docs, and also check the upgrade guide.

0reactions
lock[bot]commented, Jun 2, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Smart Way To Handle Request Validation In Laravel
Laravel has Form Request, A separate request class containing validation logic. To create one you can use below Artisan command. Laravel Form  ......
Read more >
Configure Request Filtering in IIS - Microsoft Learn
Open IIS Manager and select the level for which you want to configure request filter. In Features View, double-click Request Filtering.
Read more >
Validation - Laravel - The PHP Framework For Web Artisans
Each form request generated by Laravel has two methods: authorize and rules . * Get the validation rules that apply to the request....
Read more >
Filtering and retrieving data using Amazon S3 Select
With Amazon S3 Select, you can use simple structured query language (SQL) statements to filter the contents of an Amazon S3 object and...
Read more >
Use conditions in Realtime Database Security Rules - Firebase
If your app uses Firebase Authentication, the request.auth variable contains the ... the format and content of data should be done using .validate...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found