<commit_before>"use strict";

var _ = require('lodash');
var path = require('path');
var Schema = require('node-verifier-schema');
var iterate = require('async-iterate');

var defaultsOptions = {
	cwd: process.cwd(),

	getData: function (name) {
		return function (req, res) {
			return req[name];
		};
	},

	callback: function (err, req, res, next) {
		next(err);
	},

	preparePath: function (filepath) {
		return path.join(this.cwd, filepath);
	},

	prepareSchema: function (schema, options) {
		var _rawSchema = schema;
		if (_.isString(schema)) {
			if (/\.(yaml|yml)$/.test(schema)) {
				schema = options.preparePath(schema);
				schema = expressVerifier.Schema.load(schema);
			} else {
				schema = expressVerifier.Schema.get(schema);
			}
		} else if (_.isFunction(schema)) {
			schema = new expressVerifier.Schema();
			_rawSchema.call(schema, schema.required.bind(schema), schema.optional.bind(schema));
		}

		if (!(schema instanceof expressVerifier.Schema)) {
			throw new Error('must be Schema instance. ' + _rawSchema + ' given');
		}

		return schema;
	}
};

var expressVerifier = function (globalOptions) {
	var verifier = function (schems, options) {
		if (_.isFunction(options)) {
			options = {
				callback: options
			};
		}

		options = _.extend({}, defaultsOptions, globalOptions, options);

		if (!_.isPlainObject(schems)) {
			schems = options.prepareSchema(schems, options).fields;
		}

		var middleware = function (req, res, next) {
			iterate.each(middleware.verifiers, function (vierifier, _index, done) {
				vierifier.verify(req, res, function (err) {
					if (err instanceof expressVerifier.Schema.ValidationError) {
						err.object = vierifier.name;
					}

					done(err);
				});
			}, function (err) {
				options.callback(err, req, res, next);
			});
		};

		middleware.verifiers = _.transform(verifier.components, function(verifiers, component, name) {
			if (!_.has(schems, name)) {
				return;
			}

			verifiers.push({
				name: name,
				verify: component(schems[name], options)
			});
		}, []);

		if (!middleware.verifiers.length) {
			throw new Error('invalid parameters for verify');
		}

		return middleware;
	};

	verifier.components = {};

	_.each(['params', 'headers', 'query', 'body'], function (name) {
		verifier.components[name] = function (schema, options) {
			var middleware = function (req, res, next) {
				middleware.verifier.verify(middleware.getData(req, res), next);
			};

			middleware.schema = options.prepareSchema(schema, options);
			middleware.verifier = middleware.schema.verifier(options);
			middleware.getData = options.getData(name);

			return middleware;
		};

		verifier[name] = function (object, options) {
			var obj = {};
			obj[name] = object;
			return verifier(obj, options);
		};
	});

	return verifier;
};

expressVerifier.Schema = Schema;

module.exports = expressVerifier;<commit_msg>move componentNames to global options<commit_after>"use strict";

var _ = require('lodash');
var path = require('path');
var Schema = require('node-verifier-schema');
var iterate = require('async-iterate');

var defaultsOptions = {
	cwd: process.cwd(),

	componentNames: [
		'params',
		'headers',
		'query',
		'body'
	],

	getData: function (name) {
		return function (req, res) {
			return req[name];
		};
	},

	callback: function (err, req, res, next) {
		next(err);
	},

	preparePath: function (filepath) {
		return path.join(this.cwd, filepath);
	},

	prepareSchema: function (schema, options) {
		var _rawSchema = schema;
		if (_.isString(schema)) {
			if (/\.(yaml|yml)$/.test(schema)) {
				schema = options.preparePath(schema);
				schema = expressVerifier.Schema.load(schema);
			} else {
				schema = expressVerifier.Schema.get(schema);
			}
		} else if (_.isFunction(schema)) {
			schema = new expressVerifier.Schema();
			_rawSchema.call(schema, schema.required.bind(schema), schema.optional.bind(schema));
		}

		if (!(schema instanceof expressVerifier.Schema)) {
			throw new Error('must be Schema instance. ' + _rawSchema + ' given');
		}

		return schema;
	}
};

var expressVerifier = function (globalOptions) {
	globalOptions = _.extend({}, defaultsOptions, globalOptions);

	var verifier = function (schems, options) {
		if (_.isFunction(options)) {
			options = {
				callback: options
			};
		}

		options = _.extend({}, globalOptions, options);

		if (!_.isPlainObject(schems)) {
			schems = options.prepareSchema(schems, options).fields;
		}

		var middleware = function (req, res, next) {
			iterate.each(middleware.verifiers, function (vierifier, _index, done) {
				vierifier.verify(req, res, function (err) {
					if (err instanceof expressVerifier.Schema.ValidationError) {
						err.object = vierifier.name;
					}

					done(err);
				});
			}, function (err) {
				options.callback(err, req, res, next);
			});
		};

		middleware.verifiers = _.transform(verifier.components, function(verifiers, component, name) {
			if (!_.has(schems, name)) {
				return;
			}

			verifiers.push({
				name: name,
				verify: component(schems[name], options)
			});
		}, []);

		if (!middleware.verifiers.length) {
			throw new Error('invalid parameters for verify');
		}

		return middleware;
	};

	verifier.components = {};

	_.each(globalOptions.componentNames, function (name) {
		verifier.components[name] = function (schema, options) {
			var middleware = function (req, res, next) {
				middleware.verifier.verify(middleware.getData(req, res), next);
			};

			middleware.schema = options.prepareSchema(schema, options);
			middleware.verifier = middleware.schema.verifier(options);
			middleware.getData = options.getData(name);

			return middleware;
		};

		verifier[name] = function (object, options) {
			var obj = {};
			obj[name] = object;
			return verifier(obj, options);
		};
	});

	return verifier;
};

expressVerifier.Schema = Schema;

module.exports = expressVerifier;
{'fraction_non_alphanumeric': 0.15090166723375298, 'fraction_numerical': 0.0, 'mean_word_length': 3.8109656301145662, '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': '4761915', 'n_tokens_mistral': 2168, 'n_tokens_neox': 1858, 'n_words': 414}