diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index 07f9b1fb95..c4404dcff8 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -15,12 +15,10 @@ use Utopia\Database\Validator\Key; use Utopia\Database\Validator\Permissions; use Utopia\Database\Validator\QueryValidator; use Utopia\Database\Validator\Queries as QueriesValidator; +use Utopia\Database\Validator\Structure; use Utopia\Database\Validator\UID; use Utopia\Database\Exception\Authorization as AuthorizationException; use Utopia\Database\Exception\Structure as StructureException; -use Appwrite\Network\Validator\Email; -use Appwrite\Network\Validator\IP; -use Appwrite\Network\Validator\URL; use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; @@ -244,7 +242,7 @@ App::post('/v1/database/collections/:collectionId/attributes/string') ->param('size', null, new Integer(), 'Attribute size for text attributes, in number of characters.') ->param('required', null, new Boolean(), 'Is attribute required?') ->param('default', null, new Text(0), 'Default value for attribute when not provided. Cannot be set when attribute is required.', true) - ->param('format', null, new Whitelist(['email', 'ip', 'url']), 'Optional format validation of attribute. Must be one of (email, ip, url).', true) + ->param('format', null, new Whitelist(['email', 'ip', 'url']), 'Optional format validation of attribute. Must be one of: email, ip, url', true) ->param('array', false, new Boolean(), 'Is attribute an array?', true) ->inject('response') ->inject('dbForExternal') @@ -271,6 +269,10 @@ App::post('/v1/database/collections/:collectionId/attributes/string') throw new Exception('Length of default attribute exceeds attribute size', 400); } + if (!Structure::hasFormat($format, $type)) { + throw new Exception("Format {$format} not available for {$type} attributes.", 400); + } + // integers are signed by default, and filters are hidden from the endpoint. $signed = true; $filters = []; @@ -945,38 +947,8 @@ App::post('/v1/database/collections/:collectionId/documents') $data['$read'] = (is_null($read) && !$user->isEmpty()) ? ['user:'.$user->getId()] : $read ?? []; // By default set read permissions for user $data['$write'] = (is_null($write) && !$user->isEmpty()) ? ['user:'.$user->getId()] : $write ?? []; // By default set write permissions for user - /** @var string[] $formats */ - $formats = []; - \array_walk($collection->getAttributes()['attributes'], function ($attribute) use (&$formats) { - switch ($attribute['format']) { - case 'email': - $formats[] = [ - 'name' => 'email', - 'validator' => new Email(), - 'type' => Database::VAR_STRING, - ]; - break; - case 'ip': - $formats[] = [ - 'name' => 'ip', - 'validator' => new IP(), - 'type' => Database::VAR_STRING, - ]; - break; - case 'url': - $formats[] = [ - 'name' => 'url', - 'validator' => new URL(), - 'type' => Database::VAR_STRING, - ]; - break; - default: - break; - } - }); - try { - $document = $dbForExternal->createDocument($collectionId, new Document($data), $formats); + $document = $dbForExternal->createDocument($collectionId, new Document($data)); } catch (StructureException $exception) { throw new Exception($exception->getMessage(), 400); } @@ -1133,38 +1105,8 @@ App::patch('/v1/database/collections/:collectionId/documents/:documentId') $data['$read'] = (is_null($read)) ? ($document->getRead() ?? []) : $read; // By default inherit read permissions $data['$write'] = (is_null($write)) ? ($document->getWrite() ?? []) : $write; // By default inherit write permissions - /** @var string[] $formats */ - $formats = []; - \array_walk($collection->getAttribute('attributes', []), function (Document $attribute) { - switch ($attribute->getAttribute('format', '')) { - case 'email': - $formats[] = [ - 'name' => 'email', - 'validator' => new Email(), - 'type' => Database::VAR_STRING, - ]; - break; - case 'ip': - $formats[] = [ - 'name' => 'ip', - 'validator' => new IP(), - 'type' => Database::VAR_STRING, - ]; - break; - case 'url': - $formats[] = [ - 'name' => 'url', - 'validator' => new URL(), - 'type' => Database::VAR_STRING, - ]; - break; - default: - break; - } - }); - try { - $document = $dbForExternal->updateDocument($collection->getId(), $document->getId(), new Document($data), $formats); + $document = $dbForExternal->updateDocument($collection->getId(), $document->getId(), new Document($data)); } catch (AuthorizationException $exception) { throw new Exception('Unauthorized permissions', 401); } catch (StructureException $exception) { diff --git a/app/init.php b/app/init.php index 854a938dc0..fc87454826 100644 --- a/app/init.php +++ b/app/init.php @@ -26,6 +26,9 @@ use Appwrite\Database\Adapter\Redis as RedisAdapter; use Appwrite\Database\Document; use Appwrite\Database\Validator\Authorization; use Appwrite\Event\Event; +use Appwrite\Network\Validator\Email; +use Appwrite\Network\Validator\IP; +use Appwrite\Network\Validator\URL; use Appwrite\OpenSSL\OpenSSL; use Utopia\App; use Utopia\View; @@ -40,6 +43,7 @@ use Utopia\Database\Adapter\MariaDB; use Utopia\Database\Document as Document2; use Utopia\Database\Database as Database2; use Utopia\Database\Validator\Authorization as Authorization2; +use Utopia\Database\Validator\Structure; use Swoole\Database\PDOConfig; use Swoole\Database\PDOPool; use Swoole\Database\RedisConfig; @@ -185,6 +189,10 @@ Database2::addFilter('encrypt', } ); +Structure::addFormat('email', new Email(), Database2::VAR_STRING); +Structure::addFormat('ip', new IP(), Database2::VAR_STRING); +Structure::addFormat('url', new URL(), Database2::VAR_STRING); + /* * Registry */