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

Add to oneOf, anyOf, items discriminator with mapping supports #209

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
34 changes: 34 additions & 0 deletions src/Schema/Keywords/AnyOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,40 @@ public function validate($data, array $anyOf): void

$innerExceptions = [];

if (isset($this->parentSchema->discriminator->mapping, $data[$this->parentSchema->discriminator->propertyName])) {
$schemaIndex = array_search(
$data[$this->parentSchema->discriminator->propertyName],
array_keys($this->parentSchema->discriminator->mapping)
);

if ($schemaIndex === false) {
throw NotEnoughValidSchemas::fromKeywordWithInnerExceptions(
'anyOf',
$data,
$innerExceptions,
'Data must match at least one schema'
);
}

if (isset($anyOf[$schemaIndex])) {
try {
$schemaItem = $anyOf[$schemaIndex];

$schemaValidator = new SchemaValidator($this->validationDataType);
$schemaValidator->validate($data, $schemaItem, $this->dataBreadCrumb);

return;
} catch (SchemaMismatch $e) {
throw NotEnoughValidSchemas::fromKeywordWithInnerExceptions(
'anyOf',
$data,
[$e],
'Data mapped by Discriminator is not match'
);
}
}
}

foreach ($anyOf as $schema) {
$schemaValidator = new SchemaValidator($this->validationDataType);
try {
Expand Down
22 changes: 22 additions & 0 deletions src/Schema/Keywords/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use cebe\openapi\spec\Schema as CebeSchema;
use League\OpenAPIValidation\Schema\BreadCrumb;
use League\OpenAPIValidation\Schema\Exception\InvalidSchema;
use League\OpenAPIValidation\Schema\Exception\KeywordMismatch;
use League\OpenAPIValidation\Schema\Exception\SchemaMismatch;
use League\OpenAPIValidation\Schema\SchemaValidator;
use Respect\Validation\Validator;
Expand Down Expand Up @@ -52,6 +53,27 @@ public function validate($data, CebeSchema $itemsSchema): void

$schemaValidator = new SchemaValidator($this->validationDataType);
foreach ($data as $dataIndex => $dataItem) {
if (isset($itemsSchema->discriminator->mapping, $dataItem[$itemsSchema->discriminator->propertyName])) {
$discriminatorValue = $dataItem[$itemsSchema->discriminator->propertyName];
$schemaIndex = array_search($discriminatorValue, array_keys($itemsSchema->discriminator->mapping));

if ($schemaIndex === false) {
throw KeywordMismatch::fromKeyword($itemsSchema->discriminator->propertyName, $data, 'Discriminator has not mapped in schema');
}

if (isset($itemsSchema->anyOf[$schemaIndex])) {
$mappedSchemaByDiscriminator = $itemsSchema->anyOf[$schemaIndex];
$schemaValidator->validate($dataItem, $mappedSchemaByDiscriminator, $this->dataBreadCrumb->addCrumb($dataIndex));
continue;
}

if (isset($itemsSchema->oneOf[$schemaIndex])) {
$mappedSchemaByDiscriminator = $itemsSchema->oneOf[$schemaIndex];
$schemaValidator->validate($dataItem, $mappedSchemaByDiscriminator, $this->dataBreadCrumb->addCrumb($dataIndex));
continue;
}
}

$schemaValidator->validate($dataItem, $itemsSchema, $this->dataBreadCrumb->addCrumb($dataIndex));
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/Schema/Keywords/OneOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,40 @@ public function validate($data, array $oneOf): void
$innerExceptions = [];
$validSchemas = [];

if (isset($this->parentSchema->discriminator->mapping, $data[$this->parentSchema->discriminator->propertyName])) {
$schemaIndex = array_search(
$data[$this->parentSchema->discriminator->propertyName],
array_keys($this->parentSchema->discriminator->mapping)
);

if ($schemaIndex === false) {
throw NotEnoughValidSchemas::fromKeywordWithInnerExceptions(
'oneOf',
$data,
$innerExceptions,
'Data must match at least one schema'
);
}

if (isset($oneOf[$schemaIndex])) {
try {
$schemaItem = $oneOf[$schemaIndex];

$schemaValidator = new SchemaValidator($this->validationDataType);
$schemaValidator->validate($data, $schemaItem, $this->dataBreadCrumb);

return;
} catch (SchemaMismatch $e) {
throw NotEnoughValidSchemas::fromKeywordWithInnerExceptions(
'oneOf',
$data,
[$e],
'Data mapped by Discriminator is not match'
);
}
}
}

foreach ($oneOf as $schema) {
try {
$schemaValidator->validate($data, $schema, $this->dataBreadCrumb);
Expand Down
84 changes: 84 additions & 0 deletions tests/Schema/Keywords/AnyOfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,88 @@ public function testItValidatesAnyOfRed(): void
$this->assertEquals('anyOf', $e->keyword());
}
}

public function testItValidatesAnyOfGreenWithDiscriminator(): void
{
$spec = <<<SPEC
schema:
discriminator:
propertyName: type
mapping:
NAME: 0
TIME: 1
anyOf:
- type: object
properties:
type:
type: string
name:
type: string
required:
- type
- name
- type: object
properties:
type:
type: string
age:
type: integer
required:
- type
- age
SPEC;

$schema = $this->loadRawSchema($spec);
$data = [
'type' => 'NAME',
'name' => 'John',
];

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

public function testItValidatesAnyOfRedWithDiscriminator(): void
{
$spec = <<<SPEC
schema:
discriminator:
propertyName: type
mapping:
NAME: 0
TIME: 1
anyOf:
- type: object
properties:
type:
type: string
name:
type: string
required:
- type
- name
- type: object
properties:
type:
type: string
age:
type: integer
required:
- type
- age
SPEC;

$schema = $this->loadRawSchema($spec);
$data = [
'type' => 'TIME',
'age' => 'today',
];

try {
(new SchemaValidator())->validate($data, $schema);
$this->fail('Validation did not expected to pass');
} catch (KeywordMismatch $e) {
$this->assertEquals('anyOf', $e->keyword());
}
}
}
48 changes: 48 additions & 0 deletions tests/Schema/Keywords/ItemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,52 @@ public function testItValidatesItemsNestedRed(): void

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

public function testItValidatesItemsGreenWithDiscriminator(): void
{
$spec = <<<SPEC
schema:
type: array
items:
discriminator:
propertyName: type
mapping:
NAME: 0
TIME: 1
anyOf:
- type: object
properties:
type:
type: string
name:
type: string
required:
- type
- name
- type: object
properties:
type:
type: string
age:
type: integer
required:
- type
- age
SPEC;

$schema = $this->loadRawSchema($spec);
$data = [
[
'type' => 'NAME',
'name' => 'John',
],
[
'type' => 'TIME',
'age' => 22,
],
];

(new SchemaValidator())->validate($data, $schema);
$this->addToAssertionCount(2);
}
}
84 changes: 84 additions & 0 deletions tests/Schema/Keywords/OneOfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,88 @@ public function testItValidatesOneOfNoMatchesRed(): void
$this->assertEquals('oneOf', $e->keyword());
}
}

public function testItValidatesOneOfGreenWithDiscriminator(): void
{
$spec = <<<SPEC
schema:
discriminator:
propertyName: type
mapping:
NAME: 0
TIME: 1
oneOf:
- type: object
properties:
type:
type: string
name:
type: string
required:
- type
- name
- type: object
properties:
type:
type: string
age:
type: integer
required:
- type
- age
SPEC;

$schema = $this->loadRawSchema($spec);
$data = [
'type' => 'NAME',
'name' => 'John',
];

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

public function testItValidatesOneOfRedWithDiscriminator(): void
{
$spec = <<<SPEC
schema:
discriminator:
propertyName: type
mapping:
NAME: 0
TIME: 1
oneOf:
- type: object
properties:
type:
type: string
name:
type: string
required:
- type
- name
- type: object
properties:
type:
type: string
age:
type: integer
required:
- type
- age
SPEC;

$schema = $this->loadRawSchema($spec);
$data = [
'type' => 'TIME',
'age' => 'today',
];

try {
(new SchemaValidator())->validate($data, $schema);
$this->fail('Validation did not expected to pass');
} catch (KeywordMismatch $e) {
$this->assertEquals('oneOf', $e->keyword());
}
}
}