Skip to content

Commit

Permalink
message formatting and exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandr-diudiun authored Aug 8, 2022
1 parent 46a260e commit cf9d3e2
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ private static function customFormat(ValidationError $error): array
{
$formatter = new ErrorFormatter;
return [
'formattedMessage' => $formatter->formatErrorMessage($error),
'contents' => $error->schema()->info()->data(),
'formattedMessage' => "Validation error on `" . $error->data()->fullPath()[0] . "`. " . $formatter->formatErrorMessage($error) . ". But `" . $error->data()->value() . "` given.",
'properties' => $error->schema()->info()->data(),
];
}

Expand All @@ -60,18 +60,17 @@ private static function customFormatKey(): string
return 'errors';
}

public static function validateIncomeData(\stdClass $data, string $jsonFileName = ''): array
public static function validateIncomeData(\stdClass $data, string $jsonFileName = ''): void
{
$result = self::$instance->validate($data, self::$schemaUrl . '/' . $jsonFileName);
if ($result->hasError()) {
$formatter = new ErrorFormatter;
$error = $result->error();
$ErroResult = $formatter->format($error, false, self::customFormat(...), self::customFormatKey(...));
if (!isset($ErroResult['errors']['properties']->errorCode)) {
throw new \Exception("Please provide `errorCode` property to each field of validation schema", 500);
}
throw new \Exception($ErroResult['errors']['formattedMessage'], (int)$ErroResult['errors']['properties']->errorCode);
}
return [
'status' => $result->isValid(),
'data' => isset($ErroResult['errors']['formattedMessage']) ? $ErroResult['errors']['formattedMessage'] : null,
'code' => (isset($ErroResult['errors']['contents']->apiCode)) ? (int)$ErroResult['errors']['contents']->apiCode : 400
];
}
}

0 comments on commit cf9d3e2

Please sign in to comment.