Skip to content

Commit

Permalink
Validate publications with json schema
Browse files Browse the repository at this point in the history
  • Loading branch information
rjzondervan committed Nov 6, 2024
1 parent b294cf2 commit 90eac44
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 87 deletions.
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.

23 changes: 23 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,26 @@ public function jsonSerialize(): array

return $array;
}

public function getSchema(IURLGenerator $urlGenerator): object
{
$schema = [];
$schema['$schema'] = 'https://json-schema.org/draft/2020-12/schema';
$schema['$id'] = $urlGenerator->getAbsoluteURL($urlGenerator->linkToRoute('openregister.Schemas.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

0 comments on commit 90eac44

Please sign in to comment.