Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use 'object given' in TypeMismatch msg when expecting array #204

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Schema/Exception/TypeMismatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

namespace League\OpenAPIValidation\Schema\Exception;

use League\OpenAPIValidation\Foundation\ArrayHelper;

use function gettype;
use function implode;
use function is_array;
use function sprintf;

// Validation for 'type' keyword failed against a given data
Expand All @@ -19,7 +22,8 @@ class TypeMismatch extends KeywordMismatch
*/
public static function becauseTypeDoesNotMatch(array $expected, $value): self
{
$exception = new self(sprintf("Value expected to be '%s', but '%s' given.", implode(', ', $expected), gettype($value)));
$givenType = is_array($value) && ArrayHelper::isAssoc($value) ? 'object' : gettype($value);
$exception = new self(sprintf("Value expected to be '%s', but '%s' given.", implode(', ', $expected), $givenType));
$exception->data = $value;
$exception->keyword = 'type';

Expand Down
15 changes: 15 additions & 0 deletions tests/Schema/Keywords/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

namespace League\OpenAPIValidation\Tests\Schema\Keywords;

use League\OpenAPIValidation\Foundation\ArrayHelper;
use League\OpenAPIValidation\Schema\Exception\TypeMismatch;
use League\OpenAPIValidation\Schema\SchemaValidator;
use League\OpenAPIValidation\Tests\Schema\SchemaValidatorTest;
use stdClass;

use function gettype;
use function is_array;

final class TypeTest extends SchemaValidatorTest
{
/**
Expand Down Expand Up @@ -66,6 +70,15 @@ public function testItValidatesTypeRed(string $type, $invalidValue): void
$schema = $this->loadRawSchema($spec);

$this->expectException(TypeMismatch::class);
switch ($type) {
case 'array':
$expectedMsg = is_array($invalidValue) && ArrayHelper::isAssoc($invalidValue)
? "Value expected to be 'array', but 'object' given."
: "Value expected to be 'array', but '" . gettype($invalidValue) . "' given.";
$this->expectExceptionMessage($expectedMsg);
break;
}

(new SchemaValidator())->validate($invalidValue, $schema);
}

Expand All @@ -78,6 +91,8 @@ public function invalidDataProvider(): array
['string', 12],
['object', 'not object'],
['array', ['a' => 1, 'b' => 2]], // this is not a plain array (a-la JSON)
['array', [0 => 'a', 2 => 'b']], // this is not an array per JSON spec
['array', 'not array'],
['boolean', [1, 2]],
['boolean', 'True'],
['boolean', ''],
Expand Down