<commit_before><?php

namespace Hmaus\Spas\Validation\Validator;

use GuzzleHttp\Psr7\Response;
use Hmaus\Spas\Validation\ValidationError;
use Hmaus\Spas\Validation\Validator;
use Hmaus\SpasParser\ParsedRequest;
use JsonSchema\Validator as JsonSchemaValidator;

class JsonSchema implements Validator
{
    /**
     * @var bool
     */
    private $valid = false;

    /**
     * @var ValidationError[]
     */
    private $errors = [];

    /**
     * @var JsonSchemaValidator
     */
    private $jsonSchemaValidator;

    public function __construct(JsonSchemaValidator $jsonSchemaValidator)
    {
        $this->jsonSchemaValidator = $jsonSchemaValidator;
    }

    public function validate(ParsedRequest $request, Response $response)
    {
        $schema = $request->getResponse()->getSchema();

        if (empty($schema)) {
            $this->valid = true;
            return;
        }

        $decodedBody = json_decode($response->getBody()->getContents());
        $decodedSchema = json_decode($schema);

        if (null === $decodedBody) {
            $this->failEmptyBody();
            return;
        }

        $this->jsonSchemaValidator->check(
            $decodedBody,
            $decodedSchema
        );

        $this->valid = $this->jsonSchemaValidator->isValid();

        if ($this->valid) {
            return;
        }

        foreach ($this->jsonSchemaValidator->getErrors() as $schemaError) {
            $error = new ValidationError();
            $error->message = $schemaError['message'];
            $error->property = $schemaError['property'];
            $this->errors[] = $error;
        }
    }

    public function isValid() : bool
    {
        return $this->valid;
    }

    public function getId() : string
    {
        return 'json_schema';
    }

    public function getName() : string
    {
        return 'JSON Schema Validator';
    }

    public function getErrors() : array
    {
        return $this->errors;
    }

    /**
     * Set the validator state to invalid and add empty body error
     */
    private function failEmptyBody()
    {
        $this->valid = false;

        $error = new ValidationError();
        $error->property = 'root';
        $error->message = 'Expected a body according to given schema, but no body found';
        $this->errors[] = $error;
    }

    /**
     * Reset state of the validator
     *
     * E.g. clear all errors; reset `valid` property
     */
    public function reset()
    {
        $this->errors = [];
        $this->valid  = false;
    }
}<commit_msg>Reset the json schema validator object wrapped in spas' validator object<commit_after><?php

namespace Hmaus\Spas\Validation\Validator;

use GuzzleHttp\Psr7\Response;
use Hmaus\Spas\Validation\ValidationError;
use Hmaus\Spas\Validation\Validator;
use Hmaus\SpasParser\ParsedRequest;
use JsonSchema\Validator as JsonSchemaValidator;

class JsonSchema implements Validator
{
    /**
     * @var bool
     */
    private $valid = false;

    /**
     * @var ValidationError[]
     */
    private $errors = [];

    /**
     * @var JsonSchemaValidator
     */
    private $jsonSchemaValidator;

    public function __construct(JsonSchemaValidator $jsonSchemaValidator)
    {
        $this->jsonSchemaValidator = $jsonSchemaValidator;
    }

    public function validate(ParsedRequest $request, Response $response)
    {
        $schema = $request->getResponse()->getSchema();

        if (empty($schema)) {
            $this->valid = true;
            return;
        }

        $decodedBody = json_decode($response->getBody()->getContents());
        $decodedSchema = json_decode($schema);

        if (null === $decodedBody) {
            $this->failEmptyBody();
            return;
        }

        $this->jsonSchemaValidator->check(
            $decodedBody,
            $decodedSchema
        );

        $this->valid = $this->jsonSchemaValidator->isValid();

        if ($this->valid) {
            return;
        }

        foreach ($this->jsonSchemaValidator->getErrors() as $schemaError) {
            $error = new ValidationError();
            $error->message = $schemaError['message'];
            $error->property = $schemaError['property'];
            $this->errors[] = $error;
        }
    }

    public function isValid() : bool
    {
        return $this->valid;
    }

    public function getId() : string
    {
        return 'json_schema';
    }

    public function getName() : string
    {
        return 'JSON Schema Validator';
    }

    public function getErrors() : array
    {
        return $this->errors;
    }

    /**
     * Set the validator state to invalid and add empty body error
     */
    private function failEmptyBody()
    {
        $this->valid = false;

        $error = new ValidationError();
        $error->property = 'root';
        $error->message = 'Expected a body according to given schema, but no body found';
        $this->errors[] = $error;
    }

    /**
     * Reset state of the validator
     *
     * E.g. clear all errors; reset `valid` property
     */
    public function reset()
    {
        $this->errors = [];
        $this->valid  = false;
        $this->jsonSchemaValidator->reset();
    }
}
{'fraction_non_alphanumeric': 0.12428188433550363, 'fraction_numerical': 0.00038299502106472615, 'mean_word_length': 2.101543942992874, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, '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': '6425280', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1424, 'n_words': 331}