Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #453 from villamide/matched-data-separated
Implemented separated matched data for different locations
- Loading branch information
Showing
5 changed files
with
98 additions
and
16 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
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,29 @@ | ||
const _ = require('lodash'); | ||
const selectFields = require('../check/select-fields'); | ||
|
||
module.exports = (req, { onlyValidData = true } = {}) => { | ||
const validityFilter = !onlyValidData ? () => true : field => { | ||
return !req._validationErrors.find(error => | ||
error.param === field.path && | ||
error.location === field.location | ||
); | ||
}; | ||
module.exports = (req, options = {}) => { | ||
const validityFilter = createValidityFilter(req, options); | ||
const locationsFilter = createLocationFilter(options); | ||
|
||
return _(req._validationContexts) | ||
.flatMap(context => selectFields(req, context)) | ||
.filter(validityFilter) | ||
.filter(locationsFilter) | ||
.reduce((state, field) => _.set(state, field.path, field.value), {}) | ||
.valueOf(); | ||
}; | ||
}; | ||
|
||
function createValidityFilter(req, { onlyValidData }) { | ||
onlyValidData = onlyValidData === undefined ? true : onlyValidData; | ||
return !onlyValidData ? () => true : field => { | ||
return !req._validationErrors.find(error => | ||
error.param === field.path && | ||
error.location === field.location | ||
); | ||
}; | ||
} | ||
|
||
function createLocationFilter({ locations }) { | ||
const allLocations = !Array.isArray(locations) || locations.length === 0; | ||
return allLocations ? () => true : field => locations.includes(field.location); | ||
} |
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