<commit_before>var _ = require('underscore'),
	RouteSchemaManager = require('./RouteSchemaManager');

var RequestValidator = function(plugin, options) {
	options = options || {};
	var routeSchemaManager = new RouteSchemaManager({ pluginName: options.pluginName, log: options.log }),
		log = options.log || function(){},

		onRequest = function(request, next) {
			// hand routes for this server to schema manager so it can compile the schemas for each route
			// this method will not attempt to compile them more than once, so this is safe to call on every request
			try {
				routeSchemaManager.initializeRoutes(request.server.info.uri, request.server.table());
			}
			catch (error){
				log('error', 'Unable to compile and validate schemas', error);
				return next(error);
			}

			next();
		},

		onPreHandler = function(request, next) {
			var report, errorMessages,
				extractErrorMessage = function(error) {
					var message = (error.description) ? error.message + '. Description: ' + error.description : error.message;
					message += ' - on ';
					// handles array case with second replacement
					message += error.path.substr(2).replace(/\//g, '.').replace(/\.\[/g, '[');

					return message;
				};

			report = routeSchemaManager.validatePath(request);
			if (!report.valid) {
				errorMessages = 'path parameters validation error: ' +
								_.map(report.errors, function(error) { return extractErrorMessage(error); }).join(', ');
				return next(plugin.hapi.error.badRequest(errorMessages));
			}

			report = routeSchemaManager.validateQuery(request);
			if (!report.valid) {
				errorMessages = 'query parameters validation error: ' +
								_.map(report.errors, function(error) { return extractErrorMessage(error); }).join(', ');
				return next(plugin.hapi.error.badRequest(errorMessages));
			}

			report = routeSchemaManager.validateHeaders(request);<commit_msg>Handles case when error path does not exist<commit_after>var _ = require('underscore'),
	RouteSchemaManager = require('./RouteSchemaManager');

var RequestValidator = function(plugin, options) {
	options = options || {};
	var routeSchemaManager = new RouteSchemaManager({ pluginName: options.pluginName, log: options.log }),
		log = options.log || function(){},

		onRequest = function(request, next) {
			// hand routes for this server to schema manager so it can compile the schemas for each route
			// this method will not attempt to compile them more than once, so this is safe to call on every request
			try {
				routeSchemaManager.initializeRoutes(request.server.info.uri, request.server.table());
			}
			catch (error){
				log('error', 'Unable to compile and validate schemas', error);
				return next(error);
			}

			next();
		},

		onPreHandler = function(request, next) {
			var report, errorMessages,
				extractErrorMessage = function(error) {
					var message = (error.description) ? error.message + '. Description: ' + error.description : error.message;
					if (error.path){
						message += ' - on ';
						// handles array case with second replacement
						message += error.path.substr(2).replace(/\//g, '.').replace(/\.\[/g, '[');	
					}

					return message;
				};

			report = routeSchemaManager.validatePath(request);
			if (!report.valid) {
				errorMessages = 'path parameters validation error: ' +
								_.map(report.errors, function(error) { return extractErrorMessage(error); }).join(', ');
				return next(plugin.hapi.error.badRequest(errorMessages));
			}

			report = routeSchemaManager.validateQuery(request);
			if (!report.valid) {
				errorMessages = 'query parameters validation error: ' +
								_.map(report.errors, function(error) { return extractErrorMessage(error); }).join(', ');
				return next(plugin.hapi.error.badRequest(errorMessages));
			}

			report = routeSchemaManager.validateHeaders(request);
{'fraction_non_alphanumeric': 0.13283077722900963, 'fraction_numerical': 0.0005198856251624642, 'mean_word_length': 3.7860696517412937, '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': '3707313', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1040, 'n_words': 286}