<commit_before><?php namespace Illuminate\Foundation\Validation;

use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Validation\Validator;
use Illuminate\Http\Exception\HttpResponseException;

trait ValidatesRequests {

	/**
	 * Validate the given request with the given rules.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  array  $rules
	 * @return void
	 */
	public function validate(Request $request, array $rules)
	{
		$validator = $this->getValidationFactory()->make($request->all(), $rules);

		if ($validator->fails())
		{
			$this->throwValidationException($request, $validator);
		}
	}

	/**
	 * Throw the failed validation exception.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  \Illuminate\Contracts\Validation\Validator  $validator
	 * @return void
	 */
	protected function throwValidationException(Request $request, $validator)
	{
		throw new HttpResponseException($this->buildFailedValidationResponse(
			$request, $this->formatValidationErrors($validator)
		));
	}

	/**
	 * Create the response for when a request fails validation.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  array  $errors
	 * @return \Illuminate\Http\Response
	 */
	protected function buildFailedValidationResponse(Request $request, array $errors)
	{
		if ($request->ajax())
		{
			return new JsonResponse($errors, 422);
		}

		return redirect()->to($this->getRedirectUrl())
                        ->withInput($request->input())
                        ->withErrors($errors, $this->getErrorBag());
	}

	/**
	 * Format the validation errors to be returned.
	 *
	 * @param  \Illuminate\Validation\Validator  $validator
	 * @return array
	 */
	protected function formatValidationErrors(Validator $validator)
	{
		return $validator->errors()->getMessages();
	}

	/**
	 * Get the URL we should redirect to.
	 *
	 * @return string
	 */
	protected function getRedirectUrl()
	{
		return app('Illuminate\Routing\UrlGenerator')->previous();
	}

	/**
	 * Get a validation factory instance.
	 *
	 * @return \Illuminate\Contracts\Validation\Factory
	 */
	protected function getValidationFactory()
	{
		return app('Illuminate\Contracts\Validation\Factory');
	}

    /**
     * Get the key to be used for the view error bag.
     *
     * @return string
     */
    protected function getErrorBag()
    {
        return 'default';
    }

}<commit_msg>indent with tabs.<commit_after><?php namespace Illuminate\Foundation\Validation;

use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Validation\Validator;
use Illuminate\Http\Exception\HttpResponseException;

trait ValidatesRequests {

	/**
	 * Validate the given request with the given rules.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  array  $rules
	 * @return void
	 */
	public function validate(Request $request, array $rules)
	{
		$validator = $this->getValidationFactory()->make($request->all(), $rules);

		if ($validator->fails())
		{
			$this->throwValidationException($request, $validator);
		}
	}

	/**
	 * Throw the failed validation exception.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  \Illuminate\Contracts\Validation\Validator  $validator
	 * @return void
	 */
	protected function throwValidationException(Request $request, $validator)
	{
		throw new HttpResponseException($this->buildFailedValidationResponse(
			$request, $this->formatValidationErrors($validator)
		));
	}

	/**
	 * Create the response for when a request fails validation.
	 *
	 * @param  \Illuminate\Http\Request  $request
	 * @param  array  $errors
	 * @return \Illuminate\Http\Response
	 */
	protected function buildFailedValidationResponse(Request $request, array $errors)
	{
		if ($request->ajax())
		{
			return new JsonResponse($errors, 422);
		}

		return redirect()->to($this->getRedirectUrl())
						->withInput($request->input())
						->withErrors($errors, $this->getErrorBag());
	}

	/**
	 * Format the validation errors to be returned.
	 *
	 * @param  \Illuminate\Validation\Validator  $validator
	 * @return array
	 */
	protected function formatValidationErrors(Validator $validator)
	{
		return $validator->errors()->getMessages();
	}

	/**
	 * Get the URL we should redirect to.
	 *
	 * @return string
	 */
	protected function getRedirectUrl()
	{
		return app('Illuminate\Routing\UrlGenerator')->previous();
	}

	/**
	 * Get a validation factory instance.
	 *
	 * @return \Illuminate\Contracts\Validation\Factory
	 */
	protected function getValidationFactory()
	{
		return app('Illuminate\Contracts\Validation\Factory');
	}

	/**
	 * Get the key to be used for the view error bag.
	 *
	 * @return string
	 */
	protected function getErrorBag()
	{
		return 'default';
	}

}
{'fraction_non_alphanumeric': 0.12927035006326446, 'fraction_numerical': 0.0012652889076339097, 'mean_word_length': 3.6591355599214146, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '4261938', 'n_tokens_mistral': 1427, 'n_tokens_neox': 1367, 'n_words': 324}