Skip to content

Commit

Permalink
Added CustomError
Browse files Browse the repository at this point in the history
  • Loading branch information
sorinsarca committed Oct 12, 2021
1 parent dfb1367 commit 9ff2919
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
34 changes: 34 additions & 0 deletions src/Errors/CustomError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/* ============================================================================
* Copyright 2021 Zindex Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============================================================================ */

namespace Opis\JsonSchema\Errors;

use Exception;

class CustomError extends Exception
{
protected array $args;

public function __construct(string $message, array $args = []) {
parent::__construct($message);
$this->args = $args;
}

public function getArgs(): array {
return $this->args;
}
}
18 changes: 13 additions & 5 deletions src/Keywords/FiltersKeyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
Keyword,
Schema
};
use Opis\JsonSchema\Errors\ValidationError;
use Opis\JsonSchema\Errors\{ValidationError, CustomError};
use Opis\JsonSchema\Exceptions\UnresolvedFilterException;

class FiltersKeyword implements Keyword
Expand Down Expand Up @@ -62,10 +62,18 @@ public function validate(ValidationContext $context, Schema $schema): ?Validatio
$args = $context->globals();
}

if ($func instanceof Filter) {
$ok = $func->validate($context, $schema, $args);
} else {
$ok = $func($context->currentData(), $args);
try {
if ($func instanceof Filter) {
$ok = $func->validate($context, $schema, $args);
} else {
$ok = $func($context->currentData(), $args);
}
} catch (CustomError $error) {
return $this->error($schema, $context, '$filters', $error->getMessage(), $error->getArgs() + [
'filter' => $filter->name,
'type' => $type,
'args' => $args,
]);
}

if ($ok) {
Expand Down
18 changes: 13 additions & 5 deletions src/Keywords/FormatKeyword.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
Keyword,
Schema
};
use Opis\JsonSchema\Errors\ValidationError;
use Opis\JsonSchema\Errors\{ValidationError, CustomError};

class FormatKeyword implements Keyword
{
Expand Down Expand Up @@ -56,10 +56,18 @@ public function validate(ValidationContext $context, Schema $schema): ?Validatio
}

$format = $this->types[$type];
if ($format instanceof Format) {
$ok = $format->validate($context->currentData());
} else {
$ok = $format($context->currentData());

try {
if ($format instanceof Format) {
$ok = $format->validate($context->currentData());
} else {
$ok = $format($context->currentData());
}
} catch (CustomError $error) {
return $this->error($schema, $context, 'format', $error->getMessage(), $error->getArgs() + [
'format' => $this->name,
'type' => $type,
]);
}

if ($ok) {
Expand Down

0 comments on commit 9ff2919

Please sign in to comment.