From 07918dbc72202111d47be9f2b4ebd83e100c3fec Mon Sep 17 00:00:00 2001 From: Robert Zondervan Date: Fri, 8 Nov 2024 09:55:43 +0100 Subject: [PATCH] Recover from revert on validationService --- lib/Service/ValidationService.php | 113 ++++++++---------------------- 1 file changed, 28 insertions(+), 85 deletions(-) diff --git a/lib/Service/ValidationService.php b/lib/Service/ValidationService.php index 0c15c957..38371705 100644 --- a/lib/Service/ValidationService.php +++ b/lib/Service/ValidationService.php @@ -3,9 +3,18 @@ namespace OCA\OpenCatalogi\Service; use OCA\OpenCatalogi\Db\CatalogMapper; +use OCA\OpenCatalogi\Db\Publication; +use OCA\OpenCatalogi\Db\PublicationType; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Db\MultipleObjectsReturnedException; use OCP\AppFramework\OCS\OCSBadRequestException; use OCP\AppFramework\OCS\OCSNotFoundException; use OCP\IAppConfig; +use OCP\IURLGenerator; +use Opis\JsonSchema\Errors\ErrorFormatter; +use Opis\JsonSchema\Validator; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; /** * Class ValidationService @@ -14,112 +23,46 @@ */ class ValidationService { - /** - * @var string The name of the application. - */ - private string $appName; - - /** - * @var array The current MongoDB Config. - */ - private array $mongodbConfig; - /** - * ValidationService constructor. - * - * @param IAppConfig $config The application config - * @param CatalogMapper $catalogMapper The catalog mapper. - * @param ObjectService $objectService The object service. - */ public function __construct( - private readonly IAppConfig $config, - private readonly CatalogMapper $catalogMapper, private readonly ObjectService $objectService, - ) { - $this->appName = 'opencatalogi'; - - // Initialize MongoDB configuration - $this->mongodbConfig = [ - 'base_uri' => $this->config->getValueString(app: $this->appName, key: 'mongodbLocation'), - 'headers' => ['api-key' => $this->config->getValueString(app: $this->appName, key: 'mongodbKey')], - 'mongodbCluster' => $this->config->getValueString(app: $this->appName, key:'mongodbCluster') - ]; - } - - /** - * Get the MongoDB configuration. - * - * @return array The mongodb config. - */ - public function getMongodbConfig(): array + private readonly IURLGenerator $urlGenerator, + ) { - return $this->mongodbConfig; } /** - * Fetches a catalog from either the local database or mongodb + * Validate a publication to the definition defined in the PublicationType. * - * @param string $id The id of the catalog to be fetched. - * @return array The JSON Serialised catalog. + * @param Publication $publication The publication to validate. + * @return Publication The validated publication. * - * @throws OCSNotFoundException If the catalog is not found. - * @throws \GuzzleHttp\Exception\GuzzleException + * @throws DoesNotExistException + * @throws MultipleObjectsReturnedException + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ - public function getCatalog (string $id): array + public function validatePublication(array $publication): array { - // Check if MongoDB storage is enabled - if ($this->config->hasKey(app: $this->appName, key: 'mongoStorage') !== false - && $this->config->getValueString(app: $this->appName, key: 'mongoStorage') === '1' - ) { - $filter = ['id' => $id, '_schema' => 'catalog']; + $publicationTypeId = $publication['publicationType']; + $publicationType = $this->objectService->getObject(objectType: 'publicationType', id: $publicationTypeId); - try { - return $this->objectService->findObject(filters: $filter, config: $this->getMongodbConfig()); - } catch (OCSNotFoundException $exception) { - throw new OCSNotFoundException(message: 'Catalog not found for id: ' . $id); - } - } + $publicationType = (new PublicationType())->hydrate($publicationType); - // If MongoDB storage is not enabled, fetch from local database - return $this->catalogMapper->find(id: $id)->jsonSerialize(); - } + $validator = new Validator(); + $validator->setMaxErrors(100); if(empty($publicationType->getProperties()) === true) { return $publication; } $result = $validator->validate(data: (object) json_decode(json_encode($publication['data'])), schema: $publicationType->getSchema($this->urlGenerator)); - /** - * Validates a publication against the rules set for the publication. - * - * @param array $publication The publication to be validated. - * @return array The publication after it has been validated. - * - * @throws OCSBadRequestException Thrown if the object does not validate - * @throws OCSNotFoundException Thrown if the catalog is not found - */ - public function validatePublication(array $publication): array - { - // Check for required fields - $requiredFields = ['catalogi', 'publicationType']; - foreach ($requiredFields as $field) { - if (isset($publication[$field]) === false) { - throw new OCSBadRequestException(message: $field . ' is required but not given.'); - } - } - - $catalog = $publication['catalogi']; - $publicationType = $publication['publicationType']; - try { - $catalog = $this->getCatalog($catalog); - } catch (OCSNotFoundException $exception) { - throw new OCSNotFoundException(message: $exception->getMessage()); - } + $publication['validation'] = []; - // Check if the given publicationType is present in the catalog - if (in_array(needle: $publicationType, haystack: $catalog['publicationType']) === false) { - throw new OCSBadRequestException(message: 'Given publicationType object not present in catalog'); + if ($result->hasError()) { + $errors = (new ErrorFormatter())->format($result->error()); + $publication['validation'] = $errors; } return $publication;