Skip to content

Commit

Permalink
Merge pull request #10 from sunspikes/fix-uniqueitems
Browse files Browse the repository at this point in the history
Fix uniqueitems validator for multi-level items
  • Loading branch information
scaytrase authored Sep 24, 2019
2 parents a3709fe + a441834 commit 7bfd006
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 24 deletions.
11 changes: 10 additions & 1 deletion src/Schema/Keywords/UniqueItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use OpenAPIValidation\Schema\Exception\KeywordMismatch;
use Respect\Validation\Exceptions\ExceptionInterface;
use Respect\Validation\Validator;
use function array_map;
use function array_unique;
use function count;
use function var_export;

class UniqueItems extends BaseKeyword
{
Expand Down Expand Up @@ -39,7 +41,14 @@ public function validate($data, bool $uniqueItems) : void
throw InvalidSchema::becauseDefensiveSchemaValidationFailed($e);
}

if (count(array_unique($data)) !== count($data)) {
$items = $data;
if (count($data)) {
$items = array_map(static function ($item) {
return var_export($item, true);
}, $data);
}

if (count(array_unique($items)) !== count($items)) {
throw KeywordMismatch::fromKeyword('uniqueItems', $data, 'All array items must be unique');
}
}
Expand Down
213 changes: 190 additions & 23 deletions tests/Schema/Keywords/UniqueItemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,218 @@

final class UniqueItemsTest extends SchemaValidatorTest
{
public function testItValidatesUniqueItemsNoValidationGreen() : void
/**
* @return array<array<string, array<mixed>>>>
*/
public function dataProviderGreen() : array
{
$spec = <<<SPEC
return [
[
<<<SPEC
schema:
type: array
items:
type: integer
SPEC;

$schema = $this->loadRawSchema($spec);
$data = [1, 1];

(new SchemaValidator())->validate($data, $schema);
$this->addToAssertionCount(1);
SPEC
,
[],
],
[
<<<SPEC
schema:
type: array
items:
type: integer
SPEC
,
[1, 1],
],
[
<<<SPEC
schema:
type: array
items:
type: integer
uniqueItems: true
SPEC
,
[1, 2],
],
[
<<<SPEC
schema:
type: array
items:
type: number
uniqueItems: true
SPEC
,
[1, 1.0],
],
[
<<<SPEC
schema:
type: array
items:
type: boolean
uniqueItems: true
SPEC
,
[true, false],
],
[
<<<SPEC
schema:
type: array
items:
type: string
uniqueItems: true
SPEC
,
['one', 'oNe'],
],
[
<<<SPEC
schema:
type: array
items:
type: object
uniqueItems: true
SPEC
,
[(object) [1, 2], (object) [3, 4]],
],
[
<<<SPEC
schema:
type: array
items:
type: array
items:
type: object
uniqueItems: true
SPEC
,
[[(object) [1, 2], (object) [3, 4]], [(object) [1, 2], (object) [1, 2]]],
],
[
<<<SPEC
schema:
type: array
items:
type: array
items:
type: object
uniqueItems: true
uniqueItems: true
SPEC
,
[[(object) [1, 2], (object) [3, 4]], [(object) [1, 2], (object) [3, 5]]],
],
];
}

public function testItValidatesUniqueItemsGreen() : void
/**
* @return array<array<string, array<mixed>>>>
*/
public function dataProviderRed() : array
{
$spec = <<<SPEC
return [
[
<<<SPEC
schema:
type: array
items:
type: integer
uniqueItems: true
SPEC;
SPEC
,
[1, 1],
],
[
<<<SPEC
schema:
type: array
items:
type: boolean
uniqueItems: true
SPEC
,
[true, true],
],
[
<<<SPEC
schema:
type: array
items:
type: string
uniqueItems: true
SPEC
,
['one', 'one'],
],
[
<<<SPEC
schema:
type: array
items:
type: object
uniqueItems: true
SPEC
,
[(object) [1, 2], (object) [1, 2]],
],
[
<<<SPEC
schema:
type: array
items:
type: array
items:
type: object
uniqueItems: true
SPEC
,
[[(object) [1, 2], (object) [3, 4]], [(object) [1, 2], (object) [3, 4]]],
],
[
<<<SPEC
schema:
type: array
items:
type: array
items:
type: object
uniqueItems: true
uniqueItems: true
SPEC
,
[[(object) [1, 2], (object) [3, 4]], [(object) [1, 2], (object) [1, 2]]],
],
];
}

/**
* @param array<mixed> $data
*
* @dataProvider dataProviderGreen
*/
public function testsGreen(string $spec, array $data) : void
{
$schema = $this->loadRawSchema($spec);
$data = [1, 2];

(new SchemaValidator())->validate($data, $schema);
$this->addToAssertionCount(1);
}

public function testItValidatesUniqueItemsRed() : void
/**
* @param array<mixed> $data
*
* @dataProvider dataProviderRed
*/
public function testsRed(string $spec, array $data) : void
{
$spec = <<<SPEC
schema:
type: array
items:
type: integer
uniqueItems: true
SPEC;

$schema = $this->loadRawSchema($spec);
$data = [1, 1];

try {
(new SchemaValidator())->validate($data, $schema);
Expand Down

0 comments on commit 7bfd006

Please sign in to comment.