<commit_before><?php
namespace Czim\JsonApi\Http\Requests;

use Czim\JsonApi\Contracts\Support\Validation\JsonApiValidatorInterface;
use Czim\JsonApi\Data\Root;
use Czim\JsonApi\Exceptions\JsonApiValidationException;
use Czim\JsonApi\Support\Request\RequestQueryParser;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;

class JsonApiRequest extends FormRequest
{

    /**
     * @var RequestQueryParser
     */
    protected $jsonApiQuery;

    /**
     * Data object tree with input data.
     *
     * @var Root|null
     */
    protected $rootData;

    /**
     * Whether to perform JSON Schema validation for the request.
     *
     * @var bool
     */
    protected $schemaValidation = true;

    /**
     * The type of schema validation to apply.
     *
     * @var string
     */
    protected $schemaValidationType = 'request';


    /**
     * {@inheritdoc}
     */
    public function __construct(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
    {
        parent::__construct();

        $this->jsonApiQuery = new RequestQueryParser($this);
    }

    /**
     * @return RequestQueryParser
     */
    public function jsonApiQuery()
    {
        return $this->jsonApiQuery;
    }

    /**
     * Returns data object tree for request body.
     *
     * @return Root
     */
    public function data()
    {
        if ( ! $this->rootData) {
            $this->rootData = new Root($this->all());
        }

        return $this->rootData;
    }

    /**
     * Default authorization: allow.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Default rules: none.
     *
     * @return array
     */
    public function rules()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function validate()
    {
        $this->validateAgainstSchema();

        parent::validate();
    }

    /**
     * Validates the request's contents against the relevant JSON Schema.
     */
    protected function validateAgainstSchema()
    {
        if (    ! $this->schemaValidation
            ||  ! $this->schemaValidationType
            ||  ! in_array($this->getMethod(), ['PATCH', 'POST', 'PUT'])
        ) {
            return;
        }

        $validator = $this->getSchemaValidator();

        if ( ! $validator->validateSchema($this->getInputSource()->all(), $this->schemaValidationType)) {

            throw (new JsonApiValidationException('JSON-API Schema validation error'))
                ->setErrors($validator->getErrors()->toArray());
        }
    }

    /**
     * @return JsonApiValidatorInterface
     */
    protected function getSchemaValidator()
    {
        return app(JsonApiValidatorInterface::class);
    }

    /**
     * {@inheritdoc}
     *
     * @throws JsonApiValidationException
     */
    protected function failedValidation(Validator $validator)
    {
        throw (new JsonApiValidationException('The given data failed to pass validation.'))
            ->setErrors(
                $validator->getMessageBag()->toArray()
            );
    }

}
<commit_msg>Updated validation method for form request for Laravel 5.7<commit_after><?php
namespace Czim\JsonApi\Http\Requests;

use Czim\JsonApi\Contracts\Support\Validation\JsonApiValidatorInterface;
use Czim\JsonApi\Data\Root;
use Czim\JsonApi\Exceptions\JsonApiValidationException;
use Czim\JsonApi\Support\Request\RequestQueryParser;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;

class JsonApiRequest extends FormRequest
{

    /**
     * @var RequestQueryParser
     */
    protected $jsonApiQuery;

    /**
     * Data object tree with input data.
     *
     * @var Root|null
     */
    protected $rootData;

    /**
     * Whether to perform JSON Schema validation for the request.
     *
     * @var bool
     */
    protected $schemaValidation = true;

    /**
     * The type of schema validation to apply.
     *
     * @var string
     */
    protected $schemaValidationType = 'request';


    /**
     * {@inheritdoc}
     */
    public function __construct(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
    {
        parent::__construct();

        $this->jsonApiQuery = new RequestQueryParser($this);
    }

    /**
     * @return RequestQueryParser
     */
    public function jsonApiQuery()
    {
        return $this->jsonApiQuery;
    }

    /**
     * Returns data object tree for request body.
     *
     * @return Root
     */
    public function data()
    {
        if ( ! $this->rootData) {
            $this->rootData = new Root($this->all());
        }

        return $this->rootData;
    }

    /**
     * Default authorization: allow.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Default rules: none.
     *
     * @return array
     */
    public function rules()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function validateResolved()
    {
        $this->validateAgainstSchema();

        parent::validateResolved();
    }

    /**
     * Validates the request's contents against the relevant JSON Schema.
     */
    protected function validateAgainstSchema()
    {
        if (    ! $this->schemaValidation
            ||  ! $this->schemaValidationType
            ||  ! in_array($this->getMethod(), ['PATCH', 'POST', 'PUT'])
        ) {
            return;
        }

        $validator = $this->getSchemaValidator();

        if ( ! $validator->validateSchema($this->getInputSource()->all(), $this->schemaValidationType)) {

            throw (new JsonApiValidationException('JSON-API Schema validation error'))
                ->setErrors($validator->getErrors()->toArray());
        }
    }

    /**
     * @return JsonApiValidatorInterface
     */
    protected function getSchemaValidator()
    {
        return app(JsonApiValidatorInterface::class);
    }

    /**
     * {@inheritdoc}
     *
     * @throws JsonApiValidationException
     */
    protected function failedValidation(Validator $validator)
    {
        throw (new JsonApiValidationException('The given data failed to pass validation.'))
            ->setErrors(
                $validator->getMessageBag()->toArray()
            );
    }

}

{'fraction_non_alphanumeric': 0.12628510050636796, 'fraction_numerical': 0.0003068896731624981, 'mean_word_length': 2.1127029608404966, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, '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': '5698502', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1771, 'n_words': 384}