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 7e65e2d93..dcb8d3f29 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 661546157..7bb985663 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 3afcbe7aa..d3dd810fa 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 5537413b4..1ae255400 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 ecfdcc562..fcc2a57ae 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 53cdc3eb7..1aab06f76 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 36667e674..2c90eef60 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 5aea6c1b3..7b3456644 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 4cc3f1d5b..8e60ec063 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 c2adfdddd..12b88908f 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 0d5def06c..8a70ec8b0 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 ca9ba1d53..ab3a324ea 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 f746ecd75..b0f8b2307 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 523147eb2..84689b3ff 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 c0afb1f56..8101d9a30 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 bd1546e11..d26159381 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 f01673d3a..1e9313ca6 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 1d92a0ea3..84ec26555 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 18f427ee7..544482048 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; + } }