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

Validate publications with json schema #133

Merged
merged 3 commits into from
Nov 7, 2024
Merged
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@
"adbario/php-dot-notation": "^3.3.0",
"bamarni/composer-bin-plugin": "^1.8",
"elasticsearch/elasticsearch": "^v8.14.0",
"guzzlehttp/guzzle": "^7.0",
"guzzlehttp/guzzle": "^7.0",
"mpdf/mpdf": "^8.2",
"opis/json-schema": "^2.3",
"symfony/twig-bundle": "^6.4",
"symfony/uid": "^6.4"
},
Expand Down
192 changes: 191 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions lib/Db/PublicationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateTime;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
use OCP\IURLGenerator;

class PublicationType extends Entity implements JsonSerializable
{
Expand Down Expand Up @@ -122,4 +123,33 @@ public function jsonSerialize(): array

return $array;
}

/**
* Generate a JSON-Schema definition for the data field of a publication.
*
* @param IURLGenerator $urlGenerator An URL generator to generate the identifier of the schema.
*
* @return object The JSON-Schema object defining the data field of a publication.
*/
public function getSchema(IURLGenerator $urlGenerator): object
{
$schema = [];
$schema['$schema'] = 'https://json-schema.org/draft/2020-12/schema';
$schema['$id'] = $urlGenerator->getAbsoluteURL($urlGenerator->linkToRoute('opencatalogi.publication_types.show', ['id' => $this->getUuid()]));
$schema['type'] = 'object';
$schema['required'] = [];
$schema['properties'] = [];

foreach ($this->getProperties() as $name => $property) {
if ($property['required'] === true) {
$schema['required'][] = $name;
}
unset($property['title'], $property['required']);

// Remove empty fields with array_filter(), and add it to the properties of the schema.
$schema['properties'][$name] = array_filter($property);
}

return json_decode(json_encode($schema));
}
}
11 changes: 11 additions & 0 deletions lib/Service/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCP\App\IAppManager;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\IURLGenerator;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\Uid\Uuid;
Expand All @@ -34,6 +35,8 @@ class ObjectService
/** @var string $appName The name of the app */
private string $appName;

private ValidationService $validationService;

/**
* Constructor for ObjectService.
*
Expand All @@ -59,8 +62,11 @@ public function __construct(
private ContainerInterface $container,
private readonly IAppManager $appManager,
private readonly IAppConfig $config,
IURLGenerator $urlGenerator,
) {
$this->appName = 'opencatalogi';

$this->validationService = new ValidationService(objectService: $this, urlGenerator: $urlGenerator);
}

/**
Expand Down Expand Up @@ -300,6 +306,11 @@ public function saveObject(string $objectType, array $object, bool $updateVersio
{
// Get the appropriate mapper for the object type
$mapper = $this->getMapper($objectType);

if ($objectType === 'publication') {
$object = $this->validationService->validatePublication($object);
}

// If the object has an id, update it; otherwise, create a new object
if (isset($object['id']) === true) {
return $mapper->updateFromArray($object['id'], $object, $updateVersion);
Expand Down
Loading