From 20e858dbfe65add2548db414f20dc0d3fa46072f Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Tue, 13 Apr 2021 10:46:30 +0200 Subject: [PATCH] fix: validator class and spec generator --- app/controllers/mock.php | 2 +- composer.json | 4 +-- src/Appwrite/Auth/Validator/Password.php | 24 +++++++++++++ .../Database/Validator/Authorization.php | 24 +++++++++++++ .../Database/Validator/DocumentId.php | 24 +++++++++++++ src/Appwrite/Database/Validator/Key.php | 23 ++++++++++++ .../Database/Validator/Permissions.php | 23 ++++++++++++ src/Appwrite/Database/Validator/Structure.php | 24 +++++++++++++ src/Appwrite/Database/Validator/UID.php | 24 +++++++++++++ src/Appwrite/Network/Validator/CNAME.php | 24 +++++++++++++ src/Appwrite/Network/Validator/Domain.php | 24 +++++++++++++ src/Appwrite/Network/Validator/Email.php | 36 ++++++++++++------- src/Appwrite/Network/Validator/Host.php | 36 ++++++++++++------- src/Appwrite/Network/Validator/IP.php | 36 ++++++++++++------- src/Appwrite/Network/Validator/Origin.php | 24 +++++++++++++ src/Appwrite/Network/Validator/URL.php | 36 ++++++++++++------- .../Specification/Format/OpenAPI3.php | 8 +++-- .../Specification/Format/Swagger2.php | 8 +++-- src/Appwrite/Task/Validator/Cron.php | 24 +++++++++++++ 19 files changed, 373 insertions(+), 55 deletions(-) diff --git a/app/controllers/mock.php b/app/controllers/mock.php index 7e65e2d936..dcb8d3f290 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -3,12 +3,12 @@ global $utopia, $request, $response; use Appwrite\Database\Document; +use Appwrite\Network\Validator\Host; use Appwrite\Utopia\Response; use Utopia\App; use Utopia\Validator\Numeric; use Utopia\Validator\Text; use Utopia\Validator\ArrayList; -use Utopia\Validator\Host; use Utopia\Storage\Validator\File; App::get('/v1/mock/tests/foo') diff --git a/composer.json b/composer.json index 661546157f..7bb9856631 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,7 @@ "appwrite/php-clamav": "1.0.*", - "utopia-php/framework": "0.12.*", + "utopia-php/framework": "0.13.*", "utopia-php/abuse": "0.4.*", "utopia-php/analytics": "0.2.*", "utopia-php/audit": "0.5.*", @@ -50,7 +50,7 @@ "utopia-php/domains": "0.2.*", "utopia-php/swoole": "0.2.*", "utopia-php/system": "0.4.*", - "utopia-php/storage": "0.4.*", + "utopia-php/storage": "0.5.*", "utopia-php/image": "0.1.*", "resque/php-resque": "1.3.6", diff --git a/src/Appwrite/Auth/Validator/Password.php b/src/Appwrite/Auth/Validator/Password.php index 3afcbe7aa3..d3dd810fa7 100644 --- a/src/Appwrite/Auth/Validator/Password.php +++ b/src/Appwrite/Auth/Validator/Password.php @@ -40,4 +40,28 @@ class Password extends Validator return true; } + + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_STRING; + } } diff --git a/src/Appwrite/Database/Validator/Authorization.php b/src/Appwrite/Database/Validator/Authorization.php index 5537413b4d..1ae2554001 100644 --- a/src/Appwrite/Database/Validator/Authorization.php +++ b/src/Appwrite/Database/Validator/Authorization.php @@ -188,4 +188,28 @@ class Authorization extends Validator { self::$status = self::$statusDefault; } + + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_ARRAY; + } } diff --git a/src/Appwrite/Database/Validator/DocumentId.php b/src/Appwrite/Database/Validator/DocumentId.php index ecfdcc5624..fcc2a57aef 100644 --- a/src/Appwrite/Database/Validator/DocumentId.php +++ b/src/Appwrite/Database/Validator/DocumentId.php @@ -78,4 +78,28 @@ class DocumentId extends Validator return true; } + + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_STRING; + } } diff --git a/src/Appwrite/Database/Validator/Key.php b/src/Appwrite/Database/Validator/Key.php index 53cdc3eb77..1aab06f76d 100644 --- a/src/Appwrite/Database/Validator/Key.php +++ b/src/Appwrite/Database/Validator/Key.php @@ -48,4 +48,27 @@ class Key extends Validator return true; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_STRING; + } } diff --git a/src/Appwrite/Database/Validator/Permissions.php b/src/Appwrite/Database/Validator/Permissions.php index 36667e674f..2c90eef60d 100644 --- a/src/Appwrite/Database/Validator/Permissions.php +++ b/src/Appwrite/Database/Validator/Permissions.php @@ -74,4 +74,27 @@ class Permissions extends Validator return true; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_ARRAY; + } } diff --git a/src/Appwrite/Database/Validator/Structure.php b/src/Appwrite/Database/Validator/Structure.php index 5aea6c1b3b..7b3456644c 100644 --- a/src/Appwrite/Database/Validator/Structure.php +++ b/src/Appwrite/Database/Validator/Structure.php @@ -285,4 +285,28 @@ class Structure extends Validator { return $this->database->getDocument($id); } + + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_OBJECT; + } } diff --git a/src/Appwrite/Database/Validator/UID.php b/src/Appwrite/Database/Validator/UID.php index 4cc3f1d5b2..8e60ec0632 100644 --- a/src/Appwrite/Database/Validator/UID.php +++ b/src/Appwrite/Database/Validator/UID.php @@ -43,4 +43,28 @@ class UID extends Validator return true; } + + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_STRING; + } } diff --git a/src/Appwrite/Network/Validator/CNAME.php b/src/Appwrite/Network/Validator/CNAME.php index c2adfdddde..12b88908f7 100644 --- a/src/Appwrite/Network/Validator/CNAME.php +++ b/src/Appwrite/Network/Validator/CNAME.php @@ -54,4 +54,28 @@ class CNAME extends Validator return false; } + + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_STRING; + } } diff --git a/src/Appwrite/Network/Validator/Domain.php b/src/Appwrite/Network/Validator/Domain.php index 0d5def06c6..8a70ec8b03 100644 --- a/src/Appwrite/Network/Validator/Domain.php +++ b/src/Appwrite/Network/Validator/Domain.php @@ -51,4 +51,28 @@ class Domain extends Validator return true; } + + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_STRING; + } } diff --git a/src/Appwrite/Network/Validator/Email.php b/src/Appwrite/Network/Validator/Email.php index ca9ba1d53d..ab3a324ea1 100644 --- a/src/Appwrite/Network/Validator/Email.php +++ b/src/Appwrite/Network/Validator/Email.php @@ -25,18 +25,6 @@ class Email extends Validator return 'Value must be a valid email address'; } - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType() - { - return 'string'; - } - /** * Is valid * @@ -53,4 +41,28 @@ class Email extends Validator return true; } + + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_STRING; + } } diff --git a/src/Appwrite/Network/Validator/Host.php b/src/Appwrite/Network/Validator/Host.php index f746ecd752..b0f8b23073 100644 --- a/src/Appwrite/Network/Validator/Host.php +++ b/src/Appwrite/Network/Validator/Host.php @@ -35,18 +35,6 @@ class Host extends Validator return 'URL host must be one of: ' . \implode(', ', $this->whitelist); } - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType() - { - return 'string'; - } - /** * Is valid * @@ -69,4 +57,28 @@ class Host extends Validator return false; } + + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_STRING; + } } diff --git a/src/Appwrite/Network/Validator/IP.php b/src/Appwrite/Network/Validator/IP.php index 523147eb2d..84689b3ff7 100644 --- a/src/Appwrite/Network/Validator/IP.php +++ b/src/Appwrite/Network/Validator/IP.php @@ -51,18 +51,6 @@ class IP extends Validator return 'Value must be a valid IP address'; } - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType() - { - return 'string'; - } - /** * Is valid * @@ -99,4 +87,28 @@ class IP extends Validator return false; } + + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_STRING; + } } diff --git a/src/Appwrite/Network/Validator/Origin.php b/src/Appwrite/Network/Validator/Origin.php index c0afb1f56d..8101d9a30c 100644 --- a/src/Appwrite/Network/Validator/Origin.php +++ b/src/Appwrite/Network/Validator/Origin.php @@ -119,4 +119,28 @@ class Origin extends Validator return false; } + + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_STRING; + } } diff --git a/src/Appwrite/Network/Validator/URL.php b/src/Appwrite/Network/Validator/URL.php index bd1546e111..d26159381c 100644 --- a/src/Appwrite/Network/Validator/URL.php +++ b/src/Appwrite/Network/Validator/URL.php @@ -25,18 +25,6 @@ class URL extends Validator return 'Value must be a valid URL'; } - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType() - { - return 'string'; - } - /** * Is valid * @@ -53,4 +41,28 @@ class URL extends Validator return true; } + + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_STRING; + } } diff --git a/src/Appwrite/Specification/Format/OpenAPI3.php b/src/Appwrite/Specification/Format/OpenAPI3.php index f01673d3a5..1e9313ca6f 100644 --- a/src/Appwrite/Specification/Format/OpenAPI3.php +++ b/src/Appwrite/Specification/Format/OpenAPI3.php @@ -251,14 +251,18 @@ class OpenAPI3 extends Format case 'Utopia\Validator\Length': $node['schema']['type'] = 'string'; break; - case 'Utopia\Validator\Host': + case 'Appwrite\Network\Validator\Host': $node['schema']['type'] = 'string'; $node['schema']['format'] = 'url'; $node['schema']['x-example'] = 'https://example.com'; break; case 'Utopia\Validator\WhiteList': /** @var \Utopia\Validator\WhiteList $validator */ - $node['schema']['type'] = 'string'; + $node['schema']['type'] = $validator->getType(); $node['schema']['x-example'] = $validator->getList()[0]; + + if ($validator->getType() === 'integer') { + $node['format'] = 'int32'; + } break; default: $node['schema']['type'] = 'string'; diff --git a/src/Appwrite/Specification/Format/Swagger2.php b/src/Appwrite/Specification/Format/Swagger2.php index 1d92a0ea3c..84ec265550 100644 --- a/src/Appwrite/Specification/Format/Swagger2.php +++ b/src/Appwrite/Specification/Format/Swagger2.php @@ -248,14 +248,18 @@ class Swagger2 extends Format case 'Utopia\Validator\Length': $node['type'] = 'string'; break; - case 'Utopia\Validator\Host': + case 'Appwrite\Network\Validator\Host': $node['type'] = 'string'; $node['format'] = 'url'; $node['x-example'] = 'https://example.com'; break; case 'Utopia\Validator\WhiteList': /** @var \Utopia\Validator\WhiteList $validator */ - $node['type'] = 'string'; + $node['type'] = $validator->getType(); $node['x-example'] = $validator->getList()[0]; + + if ($validator->getType() === 'integer') { + $node['format'] = 'int32'; + } break; default: $node['type'] = 'string'; diff --git a/src/Appwrite/Task/Validator/Cron.php b/src/Appwrite/Task/Validator/Cron.php index 18f427ee7b..544482048e 100644 --- a/src/Appwrite/Task/Validator/Cron.php +++ b/src/Appwrite/Task/Validator/Cron.php @@ -40,4 +40,28 @@ class Cron extends Validator return true; } + + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_STRING; + } }