<commit_before>var validate            = require('validate.js');
var _                   = require('lodash');
var BadRequestHttpError = require('../errors/httpError/badRequest');

/**
 * @param {Function} paramsConfigFunction
 *
 * @returns {Function}
 */
module.exports = function(paramsConfigFunction) {
    return function(req, res, next) {
        var paramsConfig  = paramsConfigFunction(req);
        var requestParams = _.merge({}, req.query, req.body, req.params);

        var paramsNames         = Object.keys(paramsConfig.params);
        var pickedRequestParams = _.pick(requestParams, paramsNames);

        var params = _.merge({}, paramsConfig.params, pickedRequestParams);

        if (paramsConfig.filters) {
            _.forOwn(paramsConfig.filters, function(filters, paramName) {
                if (typeof filters === 'function') {
                    filters = [filters];
                }

                filters.forEach(function(filter) {
                    params[paramName] = filter(params[paramName]);
                });
            });
        }

        var promise = Promise.resolve();

        if (paramsConfig.validators) {
            promise = validate.async(params, paramsConfig.validators, {
                fullMessages: false
            });
        }

        promise
            .then(function() {
                req.parameters = params;
                next();
            })
            .catch(function(errors) {
                var err = new BadRequestHttpError();

                _.forOwn(errors, function(fieldErrors, fieldName) {
                    err.addErrorField(fieldName, fieldErrors[0]);
                });

                next(err);
            });
    };
};<commit_msg>use processParams in setParameters middleware<commit_after>var BadRequestHttpError = require('../errors/httpError/badRequest');
var processParams       = require('../processParams');
var _                   = require('lodash');

/**
 * @param {Function} paramsConfigFunction
 *
 * @returns {Function}
 */
module.exports = function(paramsConfigFunction) {
    return function(req, res, next) {
        var paramsConfig  = paramsConfigFunction(req);
        var requestParams = _.merge({}, req.query, req.body, req.params);

        processParams(requestParams, paramsConfig)
            .then(function(params) {
                req.parameters = params;
                next();
            })
            .catch(function(errors) {
                var err = new BadRequestHttpError();

                _.forOwn(errors, function(fieldErrors, fieldName) {
                    err.addErrorField(fieldName, fieldErrors[0]);
                });

                next(err);
            });
    };
};
{'fraction_non_alphanumeric': 0.1201322556943424, 'fraction_numerical': 0.0007347538574577516, 'mean_word_length': 1.848326359832636, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1760913', 'n_tokens_mistral': 692, 'n_tokens_neox': 656, 'n_words': 129}