-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CXP-1300: Adds first product meta schema (#17982)
* CXP-1300: Adds first product meta schema * CXP-1300: remove & from schema * CXP-1300: fix tests
- Loading branch information
Showing
8 changed files
with
479 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...onents/catalogs/back/src/Infrastructure/Symfony/Resources/meta-schemas/product-0.0.1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"$id": "https://api.akeneo.com/mapping/product/0.0.1/schema", | ||
"type": "object", | ||
"properties": { | ||
"$id": { "$ref": "#/$defs/$id" }, | ||
"$schema": { "$ref": "#/$defs/$schema" }, | ||
"$comment": { "$ref": "#/$defs/$comment" }, | ||
"$defs": { "$ref": "#/$defs/$defs" }, | ||
"title": { "$ref": "#/$defs/title" }, | ||
"description": { "$ref": "#/$defs/description" }, | ||
"type": { "const": "object" }, | ||
"properties": { | ||
"type": "object", | ||
"additionalProperties": { "$ref": "#/$defs/property" }, | ||
"default": {} | ||
} | ||
}, | ||
"$defs": { | ||
"$id": { | ||
"$comment": "Non-empty fragments not allowed.", | ||
"pattern": "^[^#]*#?$" | ||
}, | ||
"$schema": { "$ref": "#/$defs/uriString" }, | ||
"$comment": { | ||
"type": "string" | ||
}, | ||
"$defs": { | ||
"type": "object", | ||
"additionalProperties": { "$ref": "#/$defs/property" } | ||
}, | ||
"title": { | ||
"type": "string" | ||
}, | ||
"description": { | ||
"type": "string" | ||
}, | ||
"propertyType": { | ||
"enum": [ | ||
"string" | ||
] | ||
}, | ||
"property": { | ||
"type": "object", | ||
"properties": { | ||
"title": { "$ref": "#/$defs/title" }, | ||
"description": { "$ref": "#/$defs/description" }, | ||
"type": { "$ref": "#/$defs/propertyType" } | ||
}, | ||
"additionalProperties": false, | ||
"required": ["type"] | ||
}, | ||
"uriString": { | ||
"type": "string", | ||
"format": "uri" | ||
} | ||
}, | ||
"additionalProperties": false | ||
} |
17 changes: 17 additions & 0 deletions
17
components/catalogs/back/src/Infrastructure/Validation/ProductSchema.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Akeneo\Catalogs\Infrastructure\Validation; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
/** | ||
* @copyright 2022 Akeneo SAS (http://www.akeneo.com) | ||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | ||
* | ||
* @psalm-suppress PropertyNotSetInConstructor | ||
*/ | ||
final class ProductSchema extends Constraint | ||
{ | ||
} |
82 changes: 82 additions & 0 deletions
82
components/catalogs/back/src/Infrastructure/Validation/ProductSchemaValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Akeneo\Catalogs\Infrastructure\Validation; | ||
|
||
use Opis\JsonSchema\Errors\ErrorFormatter; | ||
use Opis\JsonSchema\Validator; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
use Symfony\Component\Validator\Exception\UnexpectedValueException; | ||
|
||
/** | ||
* @copyright 2022 Akeneo SAS (http://www.akeneo.com) | ||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | ||
* | ||
* @psalm-suppress PropertyNotSetInConstructor | ||
*/ | ||
final class ProductSchemaValidator extends ConstraintValidator | ||
{ | ||
public function __construct(private LoggerInterface $logger) | ||
{ | ||
} | ||
|
||
public function validate($value, Constraint $constraint): void | ||
{ | ||
if (!$constraint instanceof ProductSchema) { | ||
throw new UnexpectedTypeException($constraint, ProductSchema::class); | ||
} | ||
|
||
if (!\is_object($value)) { | ||
throw new UnexpectedValueException($value, 'object'); | ||
} | ||
|
||
$metaSchemaId = $value->{'$schema'} ?? null; | ||
if (null === $metaSchemaId || !\is_string($metaSchemaId)) { | ||
$this->context | ||
->buildViolation('You must provide a $schema reference.') | ||
->addViolation(); | ||
|
||
return; | ||
} | ||
|
||
$metaSchemaPath = $this->getMetaSchemaLocalPath($metaSchemaId); | ||
if (null === $metaSchemaPath) { | ||
$this->context | ||
->buildViolation('You must provide a valid $schema reference.') | ||
->addViolation(); | ||
|
||
return; | ||
} | ||
|
||
$validator = new Validator(); | ||
$resolver = $validator->resolver(); | ||
\assert(null !== $resolver); | ||
$resolver->registerFile($metaSchemaId, $metaSchemaPath); | ||
|
||
$result = $validator->validate($value, $metaSchemaId); | ||
|
||
if ($result->hasError()) { | ||
$this->context | ||
->buildViolation('You must provide a valid schema.') | ||
->addViolation(); | ||
|
||
$formatter = new ErrorFormatter(); | ||
$this->logger->debug( | ||
'A Product Mapping Schema validation failed', | ||
$formatter->formatOutput($result->error(), 'verbose') | ||
); | ||
} | ||
} | ||
|
||
private function getMetaSchemaLocalPath(string $id): ?string | ||
{ | ||
return match ($id) { | ||
'https://api.akeneo.com/mapping/product/0.0.1/schema' => __DIR__.'/../Symfony/Resources/meta-schemas/product-0.0.1.json', | ||
default => null, | ||
}; | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
.../catalogs/back/tests/Integration/Infrastructure/Validation/ProductSchemaValidatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Akeneo\Catalogs\Test\Integration\Infrastructure\Validation; | ||
|
||
use Akeneo\Catalogs\Infrastructure\Validation\ProductSchema; | ||
use Akeneo\Catalogs\Test\Integration\IntegrationTestCase; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
/** | ||
* @copyright 2022 Akeneo SAS (http://www.akeneo.com) | ||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | ||
* | ||
* @covers \Akeneo\Catalogs\Infrastructure\Validation\ProductSchema | ||
* @covers \Akeneo\Catalogs\Infrastructure\Validation\ProductSchemaValidator | ||
*/ | ||
class ProductSchemaValidatorTest extends IntegrationTestCase | ||
{ | ||
private ?ValidatorInterface $validator; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->validator = self::getContainer()->get(ValidatorInterface::class); | ||
} | ||
|
||
/** | ||
* @dataProvider validSchemaDataProvider | ||
*/ | ||
public function testItAcceptsTheSchema(string $schema): void | ||
{ | ||
$violations = $this->validator->validate( | ||
\json_decode($schema, false, 512, JSON_THROW_ON_ERROR), | ||
new ProductSchema() | ||
); | ||
|
||
$this->assertEmpty($violations); | ||
} | ||
|
||
/** | ||
* @dataProvider invalidSchemaDataProvider | ||
*/ | ||
public function testItRejectsTheSchema(string $schema): void | ||
{ | ||
$violations = $this->validator->validate( | ||
\json_decode($schema, false, 512, JSON_THROW_ON_ERROR), | ||
new ProductSchema() | ||
); | ||
|
||
$this->assertNotEmpty($violations); | ||
} | ||
|
||
public function validSchemaDataProvider(): array | ||
{ | ||
return [ | ||
'0.0.1 with valid schema' => [ | ||
'schema' => <<<'JSON_WRAP' | ||
{ | ||
"$id": "https://example.com/product", | ||
"$schema": "https://api.akeneo.com/mapping/product/0.0.1/schema", | ||
"$comment": "My first schema !", | ||
"title": "Product Mapping", | ||
"description": "JSON Schema describing the structure of products expected by our application", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"body_html": { | ||
"title": "Description", | ||
"description": "Product description in raw HTML", | ||
"type": "string" | ||
} | ||
} | ||
} | ||
JSON_WRAP, | ||
], | ||
]; | ||
} | ||
|
||
public function invalidSchemaDataProvider(): array | ||
{ | ||
return [ | ||
'0.0.1 with invalid type number' => [ | ||
'schema' => <<<'JSON_WRAP' | ||
{ | ||
"$schema": "https://api.akeneo.com/mapping/product/0.0.1/schema", | ||
"properties": { | ||
"price": { | ||
"type": "number" | ||
} | ||
} | ||
} | ||
JSON_WRAP, | ||
], | ||
'0.0.1 with missing target type' => [ | ||
'schema' => <<<'JSON_WRAP' | ||
{ | ||
"$schema": "https://api.akeneo.com/mapping/product/0.0.1/schema", | ||
"properties": { | ||
"price": {} | ||
} | ||
} | ||
JSON_WRAP, | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.