UNPKG

555 BJavaScriptView Raw
1const _ = require('lodash');
2const selectFields = require('../check/select-fields');
3
4module.exports = (req, { onlyValidData = true } = {}) => {
5 const validityFilter = !onlyValidData ? () => true : field => {
6 return !req._validationErrors.find(error =>
7 error.param === field.path &&
8 error.location === field.location
9 );
10 };
11
12 return _(req._validationContexts)
13 .flatMap(context => selectFields(req, context))
14 .filter(validityFilter)
15 .reduce((state, field) => _.set(state, field.path, field.value), {})
16 .valueOf();
17};
\No newline at end of file