Skip to content

Commit 012b918

Browse files
committed
More fixes
1 parent b07f5a8 commit 012b918

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/OpenApiValidation.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,16 @@ public function __construct($schema, array $options = [])
8181
}
8282

8383
$this->validator = new Validator();
84+
$this->validator->setMaxErrors(99);
8485

8586
$this->formatResolver = $this->validator->parser()->getFormatResolver();
8687

8788
// Password validator only checks that it's a string, as format=password only is a hint to the UI
8889
$this->formatResolver->register("string", "password", new OpenApiValidation\Formats\PasswordValidator());
90+
91+
92+
// Register our prime number format
93+
$formats->registerCallable("integer", "prime", $isPrime);
8994

9095
}
9196

@@ -401,7 +406,8 @@ private function validateProperties(array $properties) : ?array
401406
} catch (Exception $e) {
402407
}
403408
if (isset($result) && $result->hasError()) {
404-
$error = $this->parseErrors($result->error(), $property->name, $property->in);
409+
$error = $this->parseErrors($result->error(), $property->name, $property->in, $property);
410+
405411
foreach ($error as $parsedError) {
406412
// As all query param values are strings type errors should be discarded
407413
$discard = false;
@@ -437,7 +443,7 @@ private function validateObject(array $schema, string $value) : array
437443
try {
438444
$value = json_decode($value);
439445
$schema = json_decode(json_encode($schema, JSON_PRESERVE_ZERO_FRACTION));
440-
$result = $this->validator->validate($value, $schema);
446+
$result = $this->validator->validate($value, $schema,);
441447
} catch (Exception $e) {
442448
return [[
443449
'name' => 'server',
@@ -561,7 +567,7 @@ private function error(int $code, string $message, array $errors = []) : Respons
561567
return $response->withBody((new StreamFactory())->createStream(json_encode($json, JSON_PRESERVE_ZERO_FRACTION)));
562568
}
563569

564-
private function parseErrors(ValidationError $error, $name = null, $in = null) : array
570+
private function parseErrors(ValidationError $error, $name = null, $in = null, ?Property $property = null ) : array
565571
{
566572
$errors = [];
567573
if ($error->subErrors()) {
@@ -587,12 +593,18 @@ private function parseErrors(ValidationError $error, $name = null, $in = null) :
587593
$err['name'] .= ($err['name'] && mb_strlen($err['name'])) ? '.'.array_shift($err['missing']) : array_shift($err['missing']);
588594
unset($err['missing'],$err['value']);
589595
}
596+
if ('error_enum' == $err['code'] && isset($property->schema->enum)) {
597+
$err['expected'] = $property->schema->enum;
598+
}
590599
if ('error_'.'$'.'schema' == $err['code']) {
591600
// This is a quickfix as the opis/json-schema wont give any other error message
592601
// There should not be any other reason this error_$schema occurs
593602
$err['code'] = 'error_additional';
594603
unset($err['schema']);
595604
}
605+
if (isset($err['expected']) && !isset($err['used']) && isset($err['type'])) {
606+
$err['used'] = $err['type'];
607+
}
596608
// As the request body is parsed as an array, empty object and empty array will both be []
597609
// Remove these errors
598610
if (!$this->options['strictEmptyArrayValidation']) {

0 commit comments

Comments
 (0)