Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented separated matched data for different locaitons
- Loading branch information
villamide
committed
Oct 13, 2017
1 parent
5f84536
commit 67927e4
Showing
3 changed files
with
87 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,28 @@ | ||
const _ = require('lodash'); | ||
const selectFields = require('../check/select-fields'); | ||
|
||
module.exports = (req, { onlyValidData = true } = {}) => { | ||
module.exports = (req, options = {}) => { | ||
let onlyValidData = options.onlyValidData === undefined ? true : options.onlyValidData | ||
let customLocations = options.locations === undefined ? false : options.locations | ||
if (customLocations && !Array.isArray(customLocations)) { | ||
throw new Error('returnLocations must be an Array') | ||
} | ||
|
||
const validityFilter = !onlyValidData ? () => true : field => { | ||
return !req._validationErrors.find(error => | ||
error.param === field.path && | ||
error.location === field.location | ||
); | ||
}; | ||
|
||
const locationsFilter = !customLocations ? () => true : v => { | ||
return customLocations.indexOf(v.location) !== -1 | ||
} | ||
|
||
return _(req._validationContexts) | ||
.flatMap(context => selectFields(req, context)) | ||
.filter(validityFilter) | ||
.filter(locationsFilter) | ||
.reduce((state, field) => _.set(state, field.path, field.value), {}) | ||
.valueOf(); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters