Skip to content

Commit

Permalink
Extract interface of ValidationResult
Browse files Browse the repository at this point in the history
This allows to use mocks in tests. The constructor of `ValidationResult`
is only useful when a real validation was performed.
  • Loading branch information
Dominic Tubach committed Jul 5, 2024
1 parent 55b26f9 commit cc5bb8e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
10 changes: 5 additions & 5 deletions Civi/RemoteTools/JsonSchema/Validation/ValidationResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use Systopia\JsonSchema\Translation\NullTranslator;
use Systopia\JsonSchema\Translation\TranslatorInterface;

final class ValidationResult {
final class ValidationResult implements ValidationResultInterface {

/**
* @var array<string, mixed>
Expand Down Expand Up @@ -67,14 +67,14 @@ public function getTaggedData(): TaggedDataContainerInterface {
}

/**
* @return array<string, non-empty-array<string>>
* @return array<string, non-empty-list<string>>
*/
public function getErrorMessages(): array {
return $this->mapErrorsToMessages($this->errorCollector->getErrors());
}

/**
* @return array<string, non-empty-array<string>>
* @return array<string, non-empty-list<string>>
*/
public function getLeafErrorMessages(): array {
return $this->mapErrorsToMessages($this->errorCollector->getLeafErrors());
Expand All @@ -89,9 +89,9 @@ public function isValid(): bool {
}

/**
* @param array<string, non-empty-array<ValidationError>> $errors
* @param array<string, non-empty-list<ValidationError>> $errors
*
* @return array<string, non-empty-array<string>>
* @return array<string, non-empty-list<string>>
*/
private function mapErrorsToMessages(array $errors): array {
return array_map(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/*
* Copyright (C) 2024 SYSTOPIA GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation in version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types = 1);

namespace Civi\RemoteTools\JsonSchema\Validation;

use Systopia\JsonSchema\Tags\TaggedDataContainerInterface;

interface ValidationResultInterface {

/**
* @return array<string, mixed>
*/
public function getData(): array;

public function getTaggedData(): TaggedDataContainerInterface;

/**
* @return array<string, non-empty-list<string>>
*/
public function getErrorMessages(): array;

/**
* @return array<string, non-empty-list<string>>
*/
public function getLeafErrorMessages(): array;

public function hasErrors(): bool;

public function isValid(): bool;

}
2 changes: 1 addition & 1 deletion Civi/RemoteTools/JsonSchema/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(TranslatorInterface $translator, OpisValidator $vali
* @inheritDoc
* @throws \JsonException
*/
public function validate(JsonSchema $jsonSchema, array $data, int $maxErrors = 1): ValidationResult {
public function validate(JsonSchema $jsonSchema, array $data, int $maxErrors = 1): ValidationResultInterface {
$validationData = JsonConverter::toStdClass($data);
$errorCollector = new ErrorCollector();
$taggedDataContainer = new TaggedDataContainer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ interface ValidatorInterface {
/**
* @phpstan-param array<string, mixed> $data JSON serializable.
*/
public function validate(JsonSchema $jsonSchema, array $data, int $maxErrors = 1): ValidationResult;
public function validate(JsonSchema $jsonSchema, array $data, int $maxErrors = 1): ValidationResultInterface;

}

0 comments on commit cc5bb8e

Please sign in to comment.