From 9bd526e178c519f2b2c42618ceb9c0e44350d5d5 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Thu, 25 Feb 2021 13:08:22 -0500 Subject: [PATCH 01/29] Defined abstract getType --- src/Validator.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Validator.php b/src/Validator.php index 14795f65..cf20c528 100755 --- a/src/Validator.php +++ b/src/Validator.php @@ -32,4 +32,13 @@ abstract public function getDescription(); * @return bool */ abstract public function isValid($value); + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + abstract public function getType(); } From 0b7934ab3a1e081f612f6385fa9100fe9ba191c1 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Thu, 25 Feb 2021 13:23:24 -0500 Subject: [PATCH 02/29] Define types in parent class for inheritance --- src/Validator.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Validator.php b/src/Validator.php index cf20c528..c42dcb29 100755 --- a/src/Validator.php +++ b/src/Validator.php @@ -14,6 +14,16 @@ abstract class Validator { + + const TYPE_BOOLEAN = "boolean"; + const TYPE_INTEGER = "integer"; + const TYPE_FLOAT = "double"; /* gettype() returns "double" for historical reasons */ + const TYPE_STRING = "string"; + const TYPE_ARRAY = "array"; + const TYPE_OBJECT = "object"; + const TYPE_RESOURCE = "resource"; + const TYPE_RESOURCE_CLOSED = "resource (closed)"; + /** * Get Description * From 581358c4b86e38d7abd5f64d656a3d5c6ae5fda9 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Thu, 25 Feb 2021 14:39:36 -0500 Subject: [PATCH 03/29] Extend getType method for validators --- src/Validator/ArrayList.php | 12 ++++++++++++ src/Validator/Assoc.php | 12 ++++++++++++ src/Validator/Boolean.php | 12 ++++++++++++ src/Validator/Domain.php | 12 ++++++++++++ src/Validator/Email.php | 12 ++++++++++++ src/Validator/FloatValidator.php | 12 ++++++++++++ src/Validator/HexColor.php | 11 +++++++++++ src/Validator/Host.php | 12 ++++++++++++ src/Validator/IP.php | 12 ++++++++++++ src/Validator/Integer.php | 12 ++++++++++++ src/Validator/JSON.php | 12 ++++++++++++ src/Validator/Multiple.php | 12 ++++++++++++ src/Validator/Numeric.php | 12 ++++++++++++ src/Validator/Text.php | 12 ++++++++++++ src/Validator/URL.php | 12 ++++++++++++ src/Validator/WhiteList.php | 12 ++++++++++++ 16 files changed, 191 insertions(+) diff --git a/src/Validator/ArrayList.php b/src/Validator/ArrayList.php index 8f68379d..becc89b4 100644 --- a/src/Validator/ArrayList.php +++ b/src/Validator/ArrayList.php @@ -52,6 +52,18 @@ public function getDescription() return 'Value must be an array and ' . $this->validator->getDescription(); } + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType() + { + return self::TYPE_ARRAY; + } + /** * Is valid * diff --git a/src/Validator/Assoc.php b/src/Validator/Assoc.php index 6a55eec0..8c6bf1a0 100644 --- a/src/Validator/Assoc.php +++ b/src/Validator/Assoc.php @@ -35,6 +35,18 @@ public function getDescription() return 'Value must be a valid object.'; } + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType() + { + return self::TYPE_ARRAY; + } + /** * Is valid * diff --git a/src/Validator/Boolean.php b/src/Validator/Boolean.php index 4d39d279..9ae90e5e 100644 --- a/src/Validator/Boolean.php +++ b/src/Validator/Boolean.php @@ -51,6 +51,18 @@ public function getDescription() return 'Value must be a boolean'; } + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType() + { + return self::TYPE_BOOLEAN; + } + /** * Is valid * diff --git a/src/Validator/Domain.php b/src/Validator/Domain.php index c6ec477f..952b57f7 100644 --- a/src/Validator/Domain.php +++ b/src/Validator/Domain.php @@ -35,6 +35,18 @@ public function getDescription() return 'Value must be a valid domain'; } + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType() + { + return self::TYPE_STRING; + } + /** * Is valid * diff --git a/src/Validator/Email.php b/src/Validator/Email.php index 016a8ccb..451e8e3b 100644 --- a/src/Validator/Email.php +++ b/src/Validator/Email.php @@ -35,6 +35,18 @@ public function getDescription() return 'Value must be a valid email address'; } + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType() + { + return self::TYPE_STRING; + } + /** * Is valid * diff --git a/src/Validator/FloatValidator.php b/src/Validator/FloatValidator.php index 5bea4f7b..5f84ebd3 100755 --- a/src/Validator/FloatValidator.php +++ b/src/Validator/FloatValidator.php @@ -35,6 +35,18 @@ public function getDescription() return 'Value must be a valid float'; } + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType() + { + return self::TYPE_FLOAT; + } + /** * Is valid * diff --git a/src/Validator/HexColor.php b/src/Validator/HexColor.php index 39de9a93..5ac2305d 100644 --- a/src/Validator/HexColor.php +++ b/src/Validator/HexColor.php @@ -23,6 +23,17 @@ public function getDescription() { return 'Value must be a valid Hex color code'; } + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType() + { + return self::TYPE_STRING; + } /** * @param mixed $value diff --git a/src/Validator/Host.php b/src/Validator/Host.php index 8f0cc37c..54c9ebbd 100644 --- a/src/Validator/Host.php +++ b/src/Validator/Host.php @@ -45,6 +45,18 @@ public function getDescription() return 'URL host must be one of: ' . \implode(', ', $this->whitelist); } + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType() + { + return self::TYPE_STRING; + } + /** * Is valid * diff --git a/src/Validator/IP.php b/src/Validator/IP.php index 2cb04303..b1ea7277 100644 --- a/src/Validator/IP.php +++ b/src/Validator/IP.php @@ -61,6 +61,18 @@ public function getDescription() return 'Value must be a valid IP address'; } + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType() + { + return self::TYPE_STRING; + } + /** * Is valid * diff --git a/src/Validator/Integer.php b/src/Validator/Integer.php index b7db4b13..4f728278 100755 --- a/src/Validator/Integer.php +++ b/src/Validator/Integer.php @@ -35,6 +35,18 @@ public function getDescription() return 'Value must be a valid integer'; } + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType() + { + return self::TYPE_INTEGER; + } + /** * Is valid * diff --git a/src/Validator/JSON.php b/src/Validator/JSON.php index 810a6f43..04737aed 100644 --- a/src/Validator/JSON.php +++ b/src/Validator/JSON.php @@ -24,6 +24,18 @@ public function getDescription() return 'Value must be a valid JSON string'; } + /** + * Get Type + * + * Returns validator type + * + * @return string + */ + public function getType() + { + return self::TYPE_OBJECT; + } + /** * @param mixed $value * @return bool diff --git a/src/Validator/Multiple.php b/src/Validator/Multiple.php index 6f806411..55a643f6 100755 --- a/src/Validator/Multiple.php +++ b/src/Validator/Multiple.php @@ -80,6 +80,18 @@ public function getDescription() return $description; } + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType() + { + return self::TYPE_ARRAY; + } + /** * Is valid * diff --git a/src/Validator/Numeric.php b/src/Validator/Numeric.php index d1e3c0d0..e9d2b127 100755 --- a/src/Validator/Numeric.php +++ b/src/Validator/Numeric.php @@ -35,6 +35,18 @@ public function getDescription() return 'Value must be a valid number'; } + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType() + { + return self::TYPE_INTEGER; /* Could be a string or float though */ + } + /** * Is valid * diff --git a/src/Validator/Text.php b/src/Validator/Text.php index afe9f279..42aa145f 100644 --- a/src/Validator/Text.php +++ b/src/Validator/Text.php @@ -57,6 +57,18 @@ public function getDescription() return $message; } + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType() + { + return self::TYPE_STRING; + } + /** * Is valid * diff --git a/src/Validator/URL.php b/src/Validator/URL.php index 3f37b41e..a610928d 100644 --- a/src/Validator/URL.php +++ b/src/Validator/URL.php @@ -35,6 +35,18 @@ public function getDescription() return 'Value must be a valid URL'; } + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType() + { + return self::TYPE_STRING; + } + /** * Is valid * diff --git a/src/Validator/WhiteList.php b/src/Validator/WhiteList.php index e277c6ee..55ffb7ed 100755 --- a/src/Validator/WhiteList.php +++ b/src/Validator/WhiteList.php @@ -75,6 +75,18 @@ public function getDescription() return 'Value must be one of (' . \implode(', ', $this->list) . ')'; } + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType() + { + return self::TYPE_ARRAY; + } + /** * Is valid * From b6bddaf312f03411c7f1814642e36ab26afa2409 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Fri, 26 Feb 2021 05:20:37 -0500 Subject: [PATCH 04/29] Add tests for getType --- tests/Validator/ArrayListTest.php | 1 + tests/Validator/AssocTest.php | 1 + tests/Validator/BooleanTest.php | 1 + tests/Validator/DomainTest.php | 1 + tests/Validator/EmailTest.php | 1 + tests/Validator/FloatValidatorTest.php | 1 + tests/Validator/HexColorTest.php | 1 + tests/Validator/HostTest.php | 1 + tests/Validator/IPTest.php | 1 + tests/Validator/IntegerTest.php | 1 + tests/Validator/JSONTest.php | 1 + tests/Validator/MultipleTest.php | 1 + tests/Validator/WhiteListTest.php | 1 + 13 files changed, 13 insertions(+) diff --git a/tests/Validator/ArrayListTest.php b/tests/Validator/ArrayListTest.php index 12ddbb36..5a697756 100755 --- a/tests/Validator/ArrayListTest.php +++ b/tests/Validator/ArrayListTest.php @@ -51,5 +51,6 @@ public function testIsValid() $this->assertEquals(false, $this->arrayList2->isValid('string')); $this->assertEquals(true, $this->arrayList2->isValid([1, 2, 3])); $this->assertEquals(false, $this->arrayList2->isValid(1, '2', 3)); + $this->assertEquals($this->arrayList1->getType(), 'array'); } } diff --git a/tests/Validator/AssocTest.php b/tests/Validator/AssocTest.php index bd332f30..95a0c93e 100755 --- a/tests/Validator/AssocTest.php +++ b/tests/Validator/AssocTest.php @@ -42,5 +42,6 @@ public function testIsValid() $this->assertEquals(true, $this->assoc->isValid(["1" => 'a', "0" => 'b', "2" => 'c'])); $this->assertEquals(true, $this->assoc->isValid(["a" => 'a', "b" => 'b', "c" => 'c'])); $this->assertEquals(true, $this->assoc->isValid([])); + $this->assertEquals($this->assoc->getType(), 'array'); } } diff --git a/tests/Validator/BooleanTest.php b/tests/Validator/BooleanTest.php index 604d94a8..ca7cd962 100755 --- a/tests/Validator/BooleanTest.php +++ b/tests/Validator/BooleanTest.php @@ -60,5 +60,6 @@ public function testIsValid() $this->assertEquals(false, $this->boolean->isValid(['string', 'string'])); $this->assertEquals(false, $this->boolean->isValid('string')); $this->assertEquals(false, $this->boolean->isValid(1.2)); + $this->assertEquals($this->boolean->getType(), 'boolean'); } } diff --git a/tests/Validator/DomainTest.php b/tests/Validator/DomainTest.php index 7daeb453..f4654d37 100755 --- a/tests/Validator/DomainTest.php +++ b/tests/Validator/DomainTest.php @@ -45,5 +45,6 @@ public function testIsValid() //$this->assertEquals(false, $this->domain->isValid('string')); //$this->assertEquals(false, $this->domain->isValid(1)); //$this->assertEquals(false, $this->domain->isValid(1.2)); + $this->assertEquals($this->domain->getType(), 'string'); } } diff --git a/tests/Validator/EmailTest.php b/tests/Validator/EmailTest.php index a23083c4..03e4d043 100755 --- a/tests/Validator/EmailTest.php +++ b/tests/Validator/EmailTest.php @@ -66,5 +66,6 @@ public function testIsValid() $this->assertEquals(false, $this->email->isValid('email@-domain.com')); // Leading dash in front of domain is invalid $this->assertEquals(false, $this->email->isValid('email@111.222.333.44444')); // Invalid IP format $this->assertEquals(false, $this->email->isValid('email@domain..com')); // Multiple dot in the domain portion is invalid + $this->assertEquals($this->email->getType(), 'string'); } } diff --git a/tests/Validator/FloatValidatorTest.php b/tests/Validator/FloatValidatorTest.php index d008e76e..fae035cd 100755 --- a/tests/Validator/FloatValidatorTest.php +++ b/tests/Validator/FloatValidatorTest.php @@ -43,5 +43,6 @@ public function testIsValid() $this->assertEquals($this->validator->isValid(true), false); $this->assertEquals($this->validator->isValid('23.5'), false); $this->assertEquals($this->validator->isValid('23'), false); + $this->assertEquals($this->validator->getType(), 'double'); } } diff --git a/tests/Validator/HexColorTest.php b/tests/Validator/HexColorTest.php index f8f1fc2a..73e3aa3d 100755 --- a/tests/Validator/HexColorTest.php +++ b/tests/Validator/HexColorTest.php @@ -44,5 +44,6 @@ public function testIsValid() $this->assertEquals($this->hexColor->isValid('ffffff'), true); $this->assertEquals($this->hexColor->isValid('fff'), true); $this->assertEquals($this->hexColor->isValid('000000'), true); + $this->assertEquals($this->hexColor->getType(), 'string'); } } diff --git a/tests/Validator/HostTest.php b/tests/Validator/HostTest.php index d90dc026..dbaf83b7 100755 --- a/tests/Validator/HostTest.php +++ b/tests/Validator/HostTest.php @@ -40,5 +40,6 @@ public function testIsValid() $this->assertEquals($this->host->isValid('localhost'), false); $this->assertEquals($this->host->isValid('http://subdomain.appwrite.test/path'), true); $this->assertEquals($this->host->isValid('http://test.subdomain.appwrite.test/path'), false); + $this->assertEquals($this->host->getType(), 'string'); } } diff --git a/tests/Validator/IPTest.php b/tests/Validator/IPTest.php index e1936089..1be8f78a 100755 --- a/tests/Validator/IPTest.php +++ b/tests/Validator/IPTest.php @@ -34,6 +34,7 @@ public function testIsValidIP() $this->assertEquals($validator->isValid(null), false); $this->assertEquals($validator->isValid(true), false); $this->assertEquals($validator->isValid(false), false); + $this->assertEquals($validator->getType(), 'string'); } public function testIsValidIPALL() diff --git a/tests/Validator/IntegerTest.php b/tests/Validator/IntegerTest.php index dd8b8017..7baf8d48 100755 --- a/tests/Validator/IntegerTest.php +++ b/tests/Validator/IntegerTest.php @@ -42,5 +42,6 @@ public function testIsValid() $this->assertEquals($this->validator->isValid(null), false); $this->assertEquals($this->validator->isValid(true), false); $this->assertEquals($this->validator->isValid(false), false); + $this->assertEquals($this->validator->getType(), 'integer'); } } diff --git a/tests/Validator/JSONTest.php b/tests/Validator/JSONTest.php index 22d58009..38acb2e4 100755 --- a/tests/Validator/JSONTest.php +++ b/tests/Validator/JSONTest.php @@ -47,5 +47,6 @@ public function testIsValid() $this->assertEquals(true, $this->json->isValid(['test'])); $this->assertEquals(true, $this->json->isValid(['test' => 'demo'])); $this->assertEquals(true, $this->json->isValid('{"test": "demo"}')); + $this->assertEquals($this->json->getType(), 'string'); } } diff --git a/tests/Validator/MultipleTest.php b/tests/Validator/MultipleTest.php index a2340504..4497b079 100755 --- a/tests/Validator/MultipleTest.php +++ b/tests/Validator/MultipleTest.php @@ -41,5 +41,6 @@ public function testIsValid() $this->assertEquals($this->multiple->isValid(1), false); $this->assertEquals($this->multiple->isValid(-1), false); $this->assertEquals($this->multiple->isValid('-1'), false); + $this->assertEquals($this->multiple->getType(), 'array'); } } diff --git a/tests/Validator/WhiteListTest.php b/tests/Validator/WhiteListTest.php index b9ff86cb..34f67779 100755 --- a/tests/Validator/WhiteListTest.php +++ b/tests/Validator/WhiteListTest.php @@ -29,6 +29,7 @@ public function testIsValid() $this->assertEquals($whiteList->isValid(3), true); $this->assertEquals($whiteList->isValid(5), false); $this->assertEquals($whiteList->getList(), ['string1', 'string2', 3, 4]); + $this->assertEquals($whiteList->getType(), 'array'); $whiteList = new WhiteList(['string1', 'string2', 3, 4], false); From de68f06644088a5bd23821b3ce56f563f1702e3d Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Fri, 26 Feb 2021 05:20:53 -0500 Subject: [PATCH 05/29] Correct expected type --- src/Validator/JSON.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Validator/JSON.php b/src/Validator/JSON.php index 04737aed..58223bb3 100644 --- a/src/Validator/JSON.php +++ b/src/Validator/JSON.php @@ -33,7 +33,7 @@ public function getDescription() */ public function getType() { - return self::TYPE_OBJECT; + return self::TYPE_STRING; } /** From 902ea126038fdd3c0f2cda3f13e7d886b6398bf6 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Fri, 26 Feb 2021 06:03:55 -0500 Subject: [PATCH 06/29] Define getType as mixed --- src/Validator.php | 1 + src/Validator/Mock.php | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Validator.php b/src/Validator.php index c42dcb29..721d3c28 100755 --- a/src/Validator.php +++ b/src/Validator.php @@ -23,6 +23,7 @@ abstract class Validator const TYPE_OBJECT = "object"; const TYPE_RESOURCE = "resource"; const TYPE_RESOURCE_CLOSED = "resource (closed)"; + const TYPE_MIXED = "mixed"; /** * Get Description diff --git a/src/Validator/Mock.php b/src/Validator/Mock.php index eea088e8..ee24f443 100755 --- a/src/Validator/Mock.php +++ b/src/Validator/Mock.php @@ -35,10 +35,22 @@ public function getDescription() return 'Every input is valid'; } + /** + * Get Type + * + * Returns validator type + * + * @return string + */ + public function getType() + { + return self::TYPE_MIXED; + } + /** * Is valid * - * Validation will pass when $value is numeric. + * Validation will pass with any input. * * @param mixed $value * @return bool From 3c40822ef12c01694450143ed24b6a66d3060984 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Fri, 26 Feb 2021 06:04:27 -0500 Subject: [PATCH 07/29] Add getType to Length validator --- src/Validator/Length.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Validator/Length.php b/src/Validator/Length.php index 8db4f427..8a81d224 100755 --- a/src/Validator/Length.php +++ b/src/Validator/Length.php @@ -55,6 +55,18 @@ public function getDescription() return 'Value must be between ' . \number_format($this->min) . ' and ' . \number_format($this->max) . ' chars'; } + /** + * Get Type + * + * Returns validator type + * + * @return string + */ + public function getType() + { + return self::TYPE_INTEGER; + } + /** * Is valid * From a43cc684b208ecd3cbf2be8365ad77e27513c643 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Mon, 1 Mar 2021 14:01:33 -0500 Subject: [PATCH 08/29] Add missing validator tests --- tests/Validator/TextTest.php | 46 ++++++++++++++++++++++++++++++++++ tests/Validator/URLTest.php | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100755 tests/Validator/TextTest.php create mode 100755 tests/Validator/URLTest.php diff --git a/tests/Validator/TextTest.php b/tests/Validator/TextTest.php new file mode 100755 index 00000000..2b96b9e2 --- /dev/null +++ b/tests/Validator/TextTest.php @@ -0,0 +1,46 @@ + + * @version 1.0 RC4 + * @license The MIT License (MIT) + */ + +namespace Utopia\Validator; + +use PHPUnit\Framework\TestCase; + +class TextTest extends TestCase +{ + /** + * @var Domain + */ + protected $text = null; + + public function setUp():void + { + $this->text = new Text(10); + } + + public function tearDown():void + { + $this->text = null; + } + + public function testIsValid() + { + // Assertions + $this->assertEquals(true, $this->text->isValid('text')); + $this->assertEquals(true, $this->text->isValid('7')); + $this->assertEquals(true, $this->text->isValid('7.9')); + $this->assertEquals(true, $this->text->isValid('["seven"]')); + $this->assertEquals(false, $this->text->isValid(["seven"])); + $this->assertEquals(false, $this->text->isValid(["seven", 8, 9.0])); + $this->assertEquals(false, $this->text->isValid(false)); + } +} diff --git a/tests/Validator/URLTest.php b/tests/Validator/URLTest.php new file mode 100755 index 00000000..cd4844f7 --- /dev/null +++ b/tests/Validator/URLTest.php @@ -0,0 +1,48 @@ + + * @version 1.0 RC4 + * @license The MIT License (MIT) + */ + +namespace Utopia\Validator; + +use PHPUnit\Framework\TestCase; + +class URLTest extends TestCase +{ + /** + * @var Domain + */ + protected $url = null; + + public function setUp():void + { + $this->url = new URL(); + } + + public function tearDown():void + { + $this->url = null; + } + + public function testIsValid() + { + // Assertions + $this->assertEquals(true, $this->url->isValid('http://example.com')); + $this->assertEquals(true, $this->url->isValid('https://example.com')); + $this->assertEquals(true, $this->url->isValid('htts://example.com')); // does not validate protocol + $this->assertEquals(false, $this->url->isValid('example.com')); // though, requires some kind of protocol + $this->assertEquals(false, $this->url->isValid('http:/example.com')); + $this->assertEquals(true, $this->url->isValid('http://exa-mple.com')); + $this->assertEquals(false, $this->url->isValid('htt@s://example.com')); + $this->assertEquals(true, $this->url->isValid('http://www.example.com/foo%2\u00c2\u00a9zbar')); + $this->assertEquals(true, $this->url->isValid('http://www.example.com/?q=%3Casdf%3E')); + } +} From 9837618b8abfae398f9b3cbaebf518ab66ff1787 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 2 Mar 2021 04:41:43 -0500 Subject: [PATCH 09/29] Correct expected type --- src/Validator/Numeric.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Validator/Numeric.php b/src/Validator/Numeric.php index e9d2b127..a093a62a 100755 --- a/src/Validator/Numeric.php +++ b/src/Validator/Numeric.php @@ -44,7 +44,7 @@ public function getDescription() */ public function getType() { - return self::TYPE_INTEGER; /* Could be a string or float though */ + return self::TYPE_MIXED; } /** From abfad1a690f351dfa9f33ca0029997ee8e7ac84f Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Mon, 15 Mar 2021 13:35:15 -0400 Subject: [PATCH 10/29] Remove unused types --- src/Validator.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Validator.php b/src/Validator.php index 721d3c28..ba8cea71 100755 --- a/src/Validator.php +++ b/src/Validator.php @@ -21,8 +21,6 @@ abstract class Validator const TYPE_STRING = "string"; const TYPE_ARRAY = "array"; const TYPE_OBJECT = "object"; - const TYPE_RESOURCE = "resource"; - const TYPE_RESOURCE_CLOSED = "resource (closed)"; const TYPE_MIXED = "mixed"; /** From 526ec0797cf8f336bd818ed62e7b41cf511dcc7e Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Mon, 15 Mar 2021 14:26:44 -0400 Subject: [PATCH 11/29] Change JSON to type array for graphql --- src/Validator/JSON.php | 2 +- tests/Validator/JSONTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Validator/JSON.php b/src/Validator/JSON.php index 58223bb3..b1714ea1 100644 --- a/src/Validator/JSON.php +++ b/src/Validator/JSON.php @@ -33,7 +33,7 @@ public function getDescription() */ public function getType() { - return self::TYPE_STRING; + return self::TYPE_ARRAY; } /** diff --git a/tests/Validator/JSONTest.php b/tests/Validator/JSONTest.php index 38acb2e4..934699ff 100755 --- a/tests/Validator/JSONTest.php +++ b/tests/Validator/JSONTest.php @@ -47,6 +47,6 @@ public function testIsValid() $this->assertEquals(true, $this->json->isValid(['test'])); $this->assertEquals(true, $this->json->isValid(['test' => 'demo'])); $this->assertEquals(true, $this->json->isValid('{"test": "demo"}')); - $this->assertEquals($this->json->getType(), 'string'); + $this->assertEquals($this->json->getType(), 'array'); } } From dea859bc5cab7df4b09edcc1d9a940945ade8e71 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 23 Mar 2021 12:12:31 -0400 Subject: [PATCH 12/29] Refactor into appwrite --- src/Validator/Email.php | 66 -------------------- src/Validator/Host.php | 82 ------------------------- src/Validator/IP.php | 112 ---------------------------------- src/Validator/URL.php | 66 -------------------- tests/Validator/EmailTest.php | 71 --------------------- tests/Validator/HostTest.php | 45 -------------- tests/Validator/IPTest.php | 81 ------------------------ tests/Validator/URLTest.php | 48 --------------- 8 files changed, 571 deletions(-) delete mode 100644 src/Validator/Email.php delete mode 100644 src/Validator/Host.php delete mode 100644 src/Validator/IP.php delete mode 100644 src/Validator/URL.php delete mode 100755 tests/Validator/EmailTest.php delete mode 100755 tests/Validator/HostTest.php delete mode 100755 tests/Validator/IPTest.php delete mode 100755 tests/Validator/URLTest.php diff --git a/src/Validator/Email.php b/src/Validator/Email.php deleted file mode 100644 index 451e8e3b..00000000 --- a/src/Validator/Email.php +++ /dev/null @@ -1,66 +0,0 @@ - - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use Utopia\Validator; - -/** - * Email - * - * Validate that an variable is a valid email address - * - * @package Utopia\Validator - */ -class Email extends Validator -{ - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription() - { - return 'Value must be a valid email address'; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType() - { - return self::TYPE_STRING; - } - - /** - * Is valid - * - * Validation will pass when $value is valid email address. - * - * @param mixed $value - * @return bool - */ - public function isValid($value) - { - if (!\filter_var($value, FILTER_VALIDATE_EMAIL)) { - return false; - } - - return true; - } -} diff --git a/src/Validator/Host.php b/src/Validator/Host.php deleted file mode 100644 index 54c9ebbd..00000000 --- a/src/Validator/Host.php +++ /dev/null @@ -1,82 +0,0 @@ - - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use Utopia\Validator; - -/** - * Host - * - * Validate that a host is allowed from given whitelisted hosts list - * - * @package Utopia\Validator - */ -class Host extends Validator -{ - protected $whitelist = []; - - /** - * @param array $whitelist - */ - public function __construct(array $whitelist) - { - $this->whitelist = $whitelist; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription() - { - return 'URL host must be one of: ' . \implode(', ', $this->whitelist); - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType() - { - return self::TYPE_STRING; - } - - /** - * Is valid - * - * Validation will pass when $value starts with one of the given hosts - * - * @param mixed $value - * @return bool - */ - public function isValid($value) - { - $urlValidator = new URL(); - - if (!$urlValidator->isValid($value)) { - return false; - } - - if (\in_array(\parse_url($value, PHP_URL_HOST), $this->whitelist)) { - return true; - } - - return false; - } -} diff --git a/src/Validator/IP.php b/src/Validator/IP.php deleted file mode 100644 index b1ea7277..00000000 --- a/src/Validator/IP.php +++ /dev/null @@ -1,112 +0,0 @@ - - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use Exception; -use Utopia\Validator; - -/** - * IP - * - * Validate that an variable is a valid IP address - * - * @package Utopia\Validator - */ -class IP extends Validator -{ - const ALL = 'all'; - const V4 = 'ipv4'; - const V6 = 'ipv6'; - - /** - * @var string - */ - protected $type = self::ALL; - - /** - * Constructor - * - * Set a the type of IP check. - * - * @param string $type - */ - public function __construct(string $type = self::ALL) - { - if (!in_array($type, [self::ALL, self::V4, self::V6])) { - throw new Exception('Unsupported IP type'); - } - - $this->type = $type; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription() - { - return 'Value must be a valid IP address'; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType() - { - return self::TYPE_STRING; - } - - /** - * Is valid - * - * Validation will pass when $value is valid IP address. - * - * @param mixed $value - * @return bool - */ - public function isValid($value) - { - switch ($this->type) { - case self::ALL: - if (\filter_var($value, FILTER_VALIDATE_IP)) { - return true; - } - break; - - case self::V4: - if (\filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { - return true; - } - break; - - case self::V6: - if (\filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - return true; - } - break; - - default: - return false; - break; - } - - return false; - } -} diff --git a/src/Validator/URL.php b/src/Validator/URL.php deleted file mode 100644 index a610928d..00000000 --- a/src/Validator/URL.php +++ /dev/null @@ -1,66 +0,0 @@ - - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use Utopia\Validator; - -/** - * URL - * - * Validate that an variable is a valid URL - * - * @package Utopia\Validator - */ -class URL extends Validator -{ - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription() - { - return 'Value must be a valid URL'; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType() - { - return self::TYPE_STRING; - } - - /** - * Is valid - * - * Validation will pass when $value is valid URL. - * - * @param mixed $value - * @return bool - */ - public function isValid($value) - { - if (\filter_var($value, FILTER_VALIDATE_URL) === false) { - return false; - } - - return true; - } -} diff --git a/tests/Validator/EmailTest.php b/tests/Validator/EmailTest.php deleted file mode 100755 index 03e4d043..00000000 --- a/tests/Validator/EmailTest.php +++ /dev/null @@ -1,71 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use PHPUnit\Framework\TestCase; - -class EmailTest extends TestCase -{ - /** - * @var Email - */ - protected $email = null; - - public function setUp():void - { - $this->email = new Email(); - } - - public function tearDown():void - { - $this->email = null; - } - - public function testIsValid() - { - // Assertions - $this->assertEquals(true, $this->email->isValid('email@domain.com')); - $this->assertEquals(true, $this->email->isValid('firstname.lastname@domain.com')); - $this->assertEquals(true, $this->email->isValid('email@subdomain.domain.com')); - $this->assertEquals(true, $this->email->isValid('firstname+lastname@domain.com')); - $this->assertEquals(true, $this->email->isValid('email@[123.123.123.123]')); - $this->assertEquals(true, $this->email->isValid('"email"@domain.com')); - $this->assertEquals(true, $this->email->isValid('1234567890@domain.com')); - $this->assertEquals(true, $this->email->isValid('email@domain-one.com')); - $this->assertEquals(true, $this->email->isValid('_______@domain.com')); - $this->assertEquals(true, $this->email->isValid('email@domain.name')); - $this->assertEquals(true, $this->email->isValid('email@domain.co.jp')); - $this->assertEquals(true, $this->email->isValid('firstname-lastname@domain.com')); - $this->assertEquals(false, $this->email->isValid(false)); - $this->assertEquals(false, $this->email->isValid(['string', 'string'])); - $this->assertEquals(false, $this->email->isValid(1)); - $this->assertEquals(false, $this->email->isValid(1.2)); - $this->assertEquals(false, $this->email->isValid('plainaddress')); // Missing @ sign and domain - $this->assertEquals(false, $this->email->isValid('@domain.com')); // Missing username - $this->assertEquals(false, $this->email->isValid('#@%^%#$@#$@#.com')); // Garbage - $this->assertEquals(false, $this->email->isValid('Joe Smith ')); // Encoded html within email is invalid - $this->assertEquals(false, $this->email->isValid('email.domain.com')); // Missing @ - $this->assertEquals(false, $this->email->isValid('email@domain@domain.com')); // Two @ sign - $this->assertEquals(false, $this->email->isValid('.email@domain.com')); // Leading dot in address is not allowed - $this->assertEquals(false, $this->email->isValid('email.@domain.com')); // Trailing dot in address is not allowed - $this->assertEquals(false, $this->email->isValid('email..email@domain.com')); // Multiple dots - $this->assertEquals(false, $this->email->isValid('あいうえお@domain.com')); // Unicode char as address - $this->assertEquals(false, $this->email->isValid('email@domain.com (Joe Smith)')); // Text followed email is not allowed - $this->assertEquals(false, $this->email->isValid('email@domain')); // Missing top level domain (.com/.net/.org/etc) - $this->assertEquals(false, $this->email->isValid('email@-domain.com')); // Leading dash in front of domain is invalid - $this->assertEquals(false, $this->email->isValid('email@111.222.333.44444')); // Invalid IP format - $this->assertEquals(false, $this->email->isValid('email@domain..com')); // Multiple dot in the domain portion is invalid - $this->assertEquals($this->email->getType(), 'string'); - } -} diff --git a/tests/Validator/HostTest.php b/tests/Validator/HostTest.php deleted file mode 100755 index dbaf83b7..00000000 --- a/tests/Validator/HostTest.php +++ /dev/null @@ -1,45 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use PHPUnit\Framework\TestCase; - -class HostTest extends TestCase -{ - /** - * @var Host - */ - protected $host = null; - - public function setUp():void - { - $this->host = new Host(['appwrite.io', 'subdomain.appwrite.test', 'localhost']); - } - - public function tearDown():void - { - $this->host = null; - } - - public function testIsValid() - { - // Assertions - $this->assertEquals($this->host->isValid('https://appwrite.io/link'), true); - $this->assertEquals($this->host->isValid('https://localhost'), true); - $this->assertEquals($this->host->isValid('localhost'), false); - $this->assertEquals($this->host->isValid('http://subdomain.appwrite.test/path'), true); - $this->assertEquals($this->host->isValid('http://test.subdomain.appwrite.test/path'), false); - $this->assertEquals($this->host->getType(), 'string'); - } -} diff --git a/tests/Validator/IPTest.php b/tests/Validator/IPTest.php deleted file mode 100755 index 1be8f78a..00000000 --- a/tests/Validator/IPTest.php +++ /dev/null @@ -1,81 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use PHPUnit\Framework\TestCase; - -class IPTest extends TestCase -{ - public function tearDown():void - { - $this->validator = null; - } - - public function testIsValidIP() - { - $validator = new IP(); - - // Assertions - $this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); - $this->assertEquals($validator->isValid('109.67.204.101'), true); - $this->assertEquals($validator->isValid(23.5), false); - $this->assertEquals($validator->isValid('23.5'), false); - $this->assertEquals($validator->isValid(null), false); - $this->assertEquals($validator->isValid(true), false); - $this->assertEquals($validator->isValid(false), false); - $this->assertEquals($validator->getType(), 'string'); - } - - public function testIsValidIPALL() - { - $validator = new IP(IP::ALL); - - // Assertions - $this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); - $this->assertEquals($validator->isValid('109.67.204.101'), true); - $this->assertEquals($validator->isValid(23.5), false); - $this->assertEquals($validator->isValid('23.5'), false); - $this->assertEquals($validator->isValid(null), false); - $this->assertEquals($validator->isValid(true), false); - $this->assertEquals($validator->isValid(false), false); - } - - public function testIsValidIPV4() - { - $validator = new IP(IP::V4); - - // Assertions - $this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), false); - $this->assertEquals($validator->isValid('109.67.204.101'), true); - $this->assertEquals($validator->isValid(23.5), false); - $this->assertEquals($validator->isValid('23.5'), false); - $this->assertEquals($validator->isValid(null), false); - $this->assertEquals($validator->isValid(true), false); - $this->assertEquals($validator->isValid(false), false); - } - - public function testIsValidIPV6() - { - $validator = new IP(IP::V6); - - // Assertions - $this->assertEquals($validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); - $this->assertEquals($validator->isValid('109.67.204.101'), false); - $this->assertEquals($validator->isValid(23.5), false); - $this->assertEquals($validator->isValid('23.5'), false); - $this->assertEquals($validator->isValid(null), false); - $this->assertEquals($validator->isValid(true), false); - $this->assertEquals($validator->isValid(false), false); - } -} diff --git a/tests/Validator/URLTest.php b/tests/Validator/URLTest.php deleted file mode 100755 index cd4844f7..00000000 --- a/tests/Validator/URLTest.php +++ /dev/null @@ -1,48 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use PHPUnit\Framework\TestCase; - -class URLTest extends TestCase -{ - /** - * @var Domain - */ - protected $url = null; - - public function setUp():void - { - $this->url = new URL(); - } - - public function tearDown():void - { - $this->url = null; - } - - public function testIsValid() - { - // Assertions - $this->assertEquals(true, $this->url->isValid('http://example.com')); - $this->assertEquals(true, $this->url->isValid('https://example.com')); - $this->assertEquals(true, $this->url->isValid('htts://example.com')); // does not validate protocol - $this->assertEquals(false, $this->url->isValid('example.com')); // though, requires some kind of protocol - $this->assertEquals(false, $this->url->isValid('http:/example.com')); - $this->assertEquals(true, $this->url->isValid('http://exa-mple.com')); - $this->assertEquals(false, $this->url->isValid('htt@s://example.com')); - $this->assertEquals(true, $this->url->isValid('http://www.example.com/foo%2\u00c2\u00a9zbar')); - $this->assertEquals(true, $this->url->isValid('http://www.example.com/?q=%3Casdf%3E')); - } -} From 917188030df6db265427e4656db58aef50c8e543 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 23 Mar 2021 12:20:37 -0400 Subject: [PATCH 13/29] Deprecate validators --- src/Validator/Length.php | 88 ------------------------ src/Validator/Mock.php | 62 ----------------- src/Validator/Multiple.php | 113 ------------------------------- tests/Validator/MultipleTest.php | 46 ------------- 4 files changed, 309 deletions(-) delete mode 100755 src/Validator/Length.php delete mode 100755 src/Validator/Mock.php delete mode 100755 src/Validator/Multiple.php delete mode 100755 tests/Validator/MultipleTest.php diff --git a/src/Validator/Length.php b/src/Validator/Length.php deleted file mode 100755 index 8a81d224..00000000 --- a/src/Validator/Length.php +++ /dev/null @@ -1,88 +0,0 @@ - - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use Utopia\Validator; - -/** - * Length - * - * Validates that an length of a string. - * - * @package Utopia\Validator - */ -class Length extends Validator -{ - /** - * @var int - */ - protected $min; - - /** - * @var int - */ - protected $max; - - /** - * @param int $min - * @param int $max - */ - public function __construct($min, $max) - { - $this->min = $min; - $this->max = $max; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription() - { - return 'Value must be between ' . \number_format($this->min) . ' and ' . \number_format($this->max) . ' chars'; - } - - /** - * Get Type - * - * Returns validator type - * - * @return string - */ - public function getType() - { - return self::TYPE_INTEGER; - } - - /** - * Is valid - * - * Validation will pass when $value number is bigger or equal than $min number and lower or equal than $max. - * - * @param mixed $value - * @return bool - */ - public function isValid($value) - { - $length = \mb_strlen($value); - - if ($this->min <= $length && $this->max >= $length) { - return true; - } - - return false; - } -} diff --git a/src/Validator/Mock.php b/src/Validator/Mock.php deleted file mode 100755 index ee24f443..00000000 --- a/src/Validator/Mock.php +++ /dev/null @@ -1,62 +0,0 @@ - - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use Utopia\Validator; - -/** - * Null - * - * Validate that always validate data as valid - * - * @package Utopia\Validator - */ -class Mock extends Validator -{ - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription() - { - return 'Every input is valid'; - } - - /** - * Get Type - * - * Returns validator type - * - * @return string - */ - public function getType() - { - return self::TYPE_MIXED; - } - - /** - * Is valid - * - * Validation will pass with any input. - * - * @param mixed $value - * @return bool - */ - public function isValid($value) - { - return true; - } -} diff --git a/src/Validator/Multiple.php b/src/Validator/Multiple.php deleted file mode 100755 index 55a643f6..00000000 --- a/src/Validator/Multiple.php +++ /dev/null @@ -1,113 +0,0 @@ - - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use Utopia\Validator; - -/** - * Multiple - * - * Multiple validator is a container of multiple validations each acting as a rule. - * - * @package Utopia\Validator - */ -class Multiple extends Validator -{ - /** - * @var Validator[] - */ - protected $rules = []; - - /** - * Constructor - * - * Multiple constructor can get any number of arguments containing Validator instances using PHP func_get_args function. - * - * Example: - * - * $multiple = new Multiple($validator1, $validator2, $validator3); - */ - public function __construct() - { - // array of all method arguments - $rules = \func_get_args(); - - foreach ($rules as $rule) { - $this->addRule($rule); - } - } - - /** - * Add rule - * - * Add a new rule to the end of the rules containing array - * - * @param Validator $rule - * @return $this - */ - public function addRule(Validator $rule) - { - $this->rules[] = $rule; - - return $this; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription() - { - $description = 'Value must be:' . "\n"; - - foreach ($this->rules as $key => $rule) { - $description .= (++$key) . '. ' . $rule->getDescription() . " \n"; - } - - return $description; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType() - { - return self::TYPE_ARRAY; - } - - /** - * Is valid - * - * Validation will pass when all rules are valid if only one of the rules is invalid validation will fail. - * - * @param mixed $value - * @return bool - */ - public function isValid($value) - { - foreach ($this->rules as $rule) { /* @var $rule Validator */ - if (false === $rule->isValid($value)) { - return false; - } - } - - return true; - } -} diff --git a/tests/Validator/MultipleTest.php b/tests/Validator/MultipleTest.php deleted file mode 100755 index 4497b079..00000000 --- a/tests/Validator/MultipleTest.php +++ /dev/null @@ -1,46 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use PHPUnit\Framework\TestCase; - -class MultipleTest extends TestCase -{ - /** - * @var Multiple - */ - protected $multiple = null; - - public function setUp():void - { - $this->multiple = new Multiple(new Range(10, 20)); - $this->multiple->addRule(new Numeric()); - } - - public function tearDown():void - { - $this->multiple = null; - } - - public function testIsValid() - { - // Assertions - $this->assertEquals($this->multiple->isValid('42'), false); - $this->assertEquals($this->multiple->isValid(20), true); - $this->assertEquals($this->multiple->isValid(1), false); - $this->assertEquals($this->multiple->isValid(-1), false); - $this->assertEquals($this->multiple->isValid('-1'), false); - $this->assertEquals($this->multiple->getType(), 'array'); - } -} From c46f614bf25b88100305996dc179e9c099e24f60 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 23 Mar 2021 12:52:22 -0400 Subject: [PATCH 14/29] Add isArray method to validators --- src/Validator.php | 9 +++++++++ src/Validator/ArrayList.php | 12 ++++++++++++ src/Validator/Assoc.php | 12 ++++++++++++ src/Validator/Boolean.php | 12 ++++++++++++ src/Validator/FloatValidator.php | 12 ++++++++++++ src/Validator/HexColor.php | 13 +++++++++++++ src/Validator/Integer.php | 12 ++++++++++++ src/Validator/Numeric.php | 12 ++++++++++++ src/Validator/Range.php | 26 +++++++++++++++++++++++++- src/Validator/Text.php | 12 ++++++++++++ src/Validator/WhiteList.php | 14 +++++++++++++- 11 files changed, 144 insertions(+), 2 deletions(-) diff --git a/src/Validator.php b/src/Validator.php index ba8cea71..6addfabe 100755 --- a/src/Validator.php +++ b/src/Validator.php @@ -32,6 +32,15 @@ abstract class Validator */ abstract public function getDescription(); + /** + * Is array + * + * Returns true if an array or false if not. + * + * @return bool + */ + abstract public function isArray(); + /** * Is valid * diff --git a/src/Validator/ArrayList.php b/src/Validator/ArrayList.php index becc89b4..a59801db 100644 --- a/src/Validator/ArrayList.php +++ b/src/Validator/ArrayList.php @@ -52,6 +52,18 @@ public function getDescription() return 'Value must be an array and ' . $this->validator->getDescription(); } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return true; + } + /** * Get Type * diff --git a/src/Validator/Assoc.php b/src/Validator/Assoc.php index 8c6bf1a0..1ea4b521 100644 --- a/src/Validator/Assoc.php +++ b/src/Validator/Assoc.php @@ -35,6 +35,18 @@ public function getDescription() return 'Value must be a valid object.'; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return true; + } + /** * Get Type * diff --git a/src/Validator/Boolean.php b/src/Validator/Boolean.php index 9ae90e5e..1b5d0006 100644 --- a/src/Validator/Boolean.php +++ b/src/Validator/Boolean.php @@ -51,6 +51,18 @@ public function getDescription() return 'Value must be a boolean'; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + /** * Get Type * diff --git a/src/Validator/FloatValidator.php b/src/Validator/FloatValidator.php index 5f84ebd3..d918b669 100755 --- a/src/Validator/FloatValidator.php +++ b/src/Validator/FloatValidator.php @@ -35,6 +35,18 @@ public function getDescription() return 'Value must be a valid float'; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + /** * Get Type * diff --git a/src/Validator/HexColor.php b/src/Validator/HexColor.php index 5ac2305d..318c87ed 100644 --- a/src/Validator/HexColor.php +++ b/src/Validator/HexColor.php @@ -23,6 +23,19 @@ public function getDescription() { return 'Value must be a valid Hex color code'; } + + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + /** * Get Type * diff --git a/src/Validator/Integer.php b/src/Validator/Integer.php index 4f728278..9a8c08dc 100755 --- a/src/Validator/Integer.php +++ b/src/Validator/Integer.php @@ -35,6 +35,18 @@ public function getDescription() return 'Value must be a valid integer'; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + /** * Get Type * diff --git a/src/Validator/Numeric.php b/src/Validator/Numeric.php index a093a62a..96a60a86 100755 --- a/src/Validator/Numeric.php +++ b/src/Validator/Numeric.php @@ -35,6 +35,18 @@ public function getDescription() return 'Value must be a valid number'; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + /** * Get Type * diff --git a/src/Validator/Range.php b/src/Validator/Range.php index b323ce7c..d6b9c2c5 100755 --- a/src/Validator/Range.php +++ b/src/Validator/Range.php @@ -19,7 +19,7 @@ * * @package Utopia\Validator */ -class Range extends Numeric +class Range extends Integer { /** * @var int @@ -71,6 +71,30 @@ public function getDescription() return 'Value must be in range between ' . \number_format($this->min) . ' and ' . \number_format($this->max); } + /** + * 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() + { + return self::TYPE_INTEGER; + } + /** * Is valid * diff --git a/src/Validator/Text.php b/src/Validator/Text.php index 42aa145f..58c6ee6f 100644 --- a/src/Validator/Text.php +++ b/src/Validator/Text.php @@ -57,6 +57,18 @@ public function getDescription() return $message; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + /** * Get Type * diff --git a/src/Validator/WhiteList.php b/src/Validator/WhiteList.php index 55ffb7ed..6aea8f7c 100755 --- a/src/Validator/WhiteList.php +++ b/src/Validator/WhiteList.php @@ -63,6 +63,18 @@ public function getList(): array return $this->list; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return true; + } + /** * Get Description * @@ -84,7 +96,7 @@ public function getDescription() */ public function getType() { - return self::TYPE_ARRAY; + return self::TYPE_STRING; } /** From ff8e3a5914a5b4954f7608a557ed5df10808c511 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 23 Mar 2021 13:09:27 -0400 Subject: [PATCH 15/29] Add isArray method --- src/Validator/Domain.php | 12 ++++++++++++ src/Validator/JSON.php | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Validator/Domain.php b/src/Validator/Domain.php index 952b57f7..7dea577f 100644 --- a/src/Validator/Domain.php +++ b/src/Validator/Domain.php @@ -35,6 +35,18 @@ public function getDescription() return 'Value must be a valid domain'; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return false; + } + /** * Get Type * diff --git a/src/Validator/JSON.php b/src/Validator/JSON.php index b1714ea1..184ceb61 100644 --- a/src/Validator/JSON.php +++ b/src/Validator/JSON.php @@ -24,6 +24,18 @@ public function getDescription() return 'Value must be a valid JSON string'; } + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return true; + } + /** * Get Type * From 0accc84d2b3dd2a1b06c046dedab5872be1245f1 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 23 Mar 2021 13:10:00 -0400 Subject: [PATCH 16/29] Deprecating numeric validator test for numeric string --- tests/Validator/RangeTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Validator/RangeTest.php b/tests/Validator/RangeTest.php index b55e863d..098161ba 100755 --- a/tests/Validator/RangeTest.php +++ b/tests/Validator/RangeTest.php @@ -39,7 +39,7 @@ public function testIsValid() $this->assertEquals($this->range->isValid(1), true); $this->assertEquals($this->range->isValid(4), true); $this->assertEquals($this->range->isValid(5), true); - $this->assertEquals($this->range->isValid('5'), true); + $this->assertEquals($this->range->isValid('5'), false); $this->assertEquals($this->range->isValid(6), false); $this->assertEquals($this->range->isValid(-1), false); $this->assertEquals($this->range->getMin(), 0); From cb9224797fb82a8e6eacb56580e40288360d3f21 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 23 Mar 2021 13:10:11 -0400 Subject: [PATCH 17/29] Reorder methods for consistency --- src/Validator/WhiteList.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Validator/WhiteList.php b/src/Validator/WhiteList.php index 6aea8f7c..4b5d9941 100755 --- a/src/Validator/WhiteList.php +++ b/src/Validator/WhiteList.php @@ -64,27 +64,27 @@ public function getList(): array } /** - * Is array + * Get Description * - * Function will return true if object is array. + * Returns validator description * - * @return bool + * @return string */ - public function isArray(): bool + public function getDescription() { - return true; + return 'Value must be one of (' . \implode(', ', $this->list) . ')'; } /** - * Get Description + * Is array * - * Returns validator description + * Function will return true if object is array. * - * @return string + * @return bool */ - public function getDescription() + public function isArray(): bool { - return 'Value must be one of (' . \implode(', ', $this->list) . ')'; + return true; } /** @@ -96,7 +96,7 @@ public function getDescription() */ public function getType() { - return self::TYPE_STRING; + return self::TYPE_ARRAY; } /** From fe00f6580efdcd758a4ee668a86d4d20d9aab838 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 23 Mar 2021 13:26:42 -0400 Subject: [PATCH 18/29] Check validator type of arrayList --- src/Validator/ArrayList.php | 2 +- tests/Validator/ArrayListTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Validator/ArrayList.php b/src/Validator/ArrayList.php index a59801db..f50f024c 100644 --- a/src/Validator/ArrayList.php +++ b/src/Validator/ArrayList.php @@ -73,7 +73,7 @@ public function isArray(): bool */ public function getType() { - return self::TYPE_ARRAY; + return $this->validator->getType(); } /** diff --git a/tests/Validator/ArrayListTest.php b/tests/Validator/ArrayListTest.php index 5a697756..386f5dec 100755 --- a/tests/Validator/ArrayListTest.php +++ b/tests/Validator/ArrayListTest.php @@ -30,7 +30,7 @@ class ArrayListTest extends TestCase public function setUp():void { $this->arrayList1 = new ArrayList(new Text(100)); - $this->arrayList2 = new ArrayList(new Numeric()); + $this->arrayList2 = new ArrayList(new Integer()); } public function tearDown():void @@ -51,6 +51,6 @@ public function testIsValid() $this->assertEquals(false, $this->arrayList2->isValid('string')); $this->assertEquals(true, $this->arrayList2->isValid([1, 2, 3])); $this->assertEquals(false, $this->arrayList2->isValid(1, '2', 3)); - $this->assertEquals($this->arrayList1->getType(), 'array'); + $this->assertEquals($this->arrayList1->getType(), 'string'); } } From 9147b070a2ab7ac57f83bcc4d7906ad67a233622 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Fri, 2 Apr 2021 10:31:30 -0400 Subject: [PATCH 19/29] Domain validator unused in appwrite/appwrite --- src/Validator/Domain.php | 81 ---------------------------------- tests/Validator/DomainTest.php | 50 --------------------- 2 files changed, 131 deletions(-) delete mode 100644 src/Validator/Domain.php delete mode 100755 tests/Validator/DomainTest.php diff --git a/src/Validator/Domain.php b/src/Validator/Domain.php deleted file mode 100644 index 7dea577f..00000000 --- a/src/Validator/Domain.php +++ /dev/null @@ -1,81 +0,0 @@ - - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use Utopia\Validator; - -/** - * Domain - * - * Validate that an variable is a valid domain address - * - * @package Utopia\Validator - */ -class Domain extends Validator -{ - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription() - { - return 'Value must be a valid domain'; - } - - /** - * 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() - { - return self::TYPE_STRING; - } - - /** - * Is valid - * - * Validation will pass when $value is valid domain. - * - * Validates domain names against RFC 1034, RFC 1035, RFC 952, RFC 1123, RFC 2732, RFC 2181, and RFC 1123. - * Also specifically validate hostnames (they must start with an alphanumberic character and contain only alphanumerics or hyphens). - * - * @param mixed $value - * @return bool - */ - public function isValid($value) - { - if (\filter_var($value, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) === false) { - return false; - } - - return true; - } -} diff --git a/tests/Validator/DomainTest.php b/tests/Validator/DomainTest.php deleted file mode 100755 index f4654d37..00000000 --- a/tests/Validator/DomainTest.php +++ /dev/null @@ -1,50 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Utopia\Validator; - -use PHPUnit\Framework\TestCase; - -class DomainTest extends TestCase -{ - /** - * @var Domain - */ - protected $domain = null; - - public function setUp():void - { - $this->domain = new Domain(); - } - - public function tearDown():void - { - $this->domain = null; - } - - public function testIsValid() - { - // Assertions - $this->assertEquals(true, $this->domain->isValid('example.com')); - $this->assertEquals(true, $this->domain->isValid('subdomain.example.com')); - $this->assertEquals(true, $this->domain->isValid('localhost')); - $this->assertEquals(true, $this->domain->isValid('appwrite.io')); - $this->assertEquals(true, $this->domain->isValid('appwrite.org')); - $this->assertEquals(false, $this->domain->isValid(false)); - $this->assertEquals(false, $this->domain->isValid(['string', 'string'])); - //$this->assertEquals(false, $this->domain->isValid('string')); - //$this->assertEquals(false, $this->domain->isValid(1)); - //$this->assertEquals(false, $this->domain->isValid(1.2)); - $this->assertEquals($this->domain->getType(), 'string'); - } -} From 6bb37549acca55248b4a3794aa5c1d7723b25ac6 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 6 Apr 2021 08:08:19 -0400 Subject: [PATCH 20/29] Update JSON typing --- src/Validator/JSON.php | 4 ++-- tests/Validator/JSONTest.php | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Validator/JSON.php b/src/Validator/JSON.php index 184ceb61..9bba0b70 100644 --- a/src/Validator/JSON.php +++ b/src/Validator/JSON.php @@ -33,7 +33,7 @@ public function getDescription() */ public function isArray(): bool { - return true; + return false; } /** @@ -45,7 +45,7 @@ public function isArray(): bool */ public function getType() { - return self::TYPE_ARRAY; + return self::TYPE_OBJECT; } /** diff --git a/tests/Validator/JSONTest.php b/tests/Validator/JSONTest.php index 934699ff..c4692943 100755 --- a/tests/Validator/JSONTest.php +++ b/tests/Validator/JSONTest.php @@ -47,6 +47,7 @@ public function testIsValid() $this->assertEquals(true, $this->json->isValid(['test'])); $this->assertEquals(true, $this->json->isValid(['test' => 'demo'])); $this->assertEquals(true, $this->json->isValid('{"test": "demo"}')); - $this->assertEquals($this->json->getType(), 'array'); + $this->assertEquals($this->json->getType(), 'object'); + $this->assertEquals($this->json->isArray(), false); } } From eab7c24a42d9bc274f706b24a941501ba498525f Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 6 Apr 2021 08:09:35 -0400 Subject: [PATCH 21/29] Whitelists have type string by default --- src/Validator/WhiteList.php | 11 +++++++++-- tests/Validator/WhiteListTest.php | 3 ++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Validator/WhiteList.php b/src/Validator/WhiteList.php index 4b5d9941..45df98a4 100755 --- a/src/Validator/WhiteList.php +++ b/src/Validator/WhiteList.php @@ -33,6 +33,11 @@ class WhiteList extends Validator */ protected $strict; + /** + * @var string + */ + protected $type; + /** * Constructor * @@ -40,11 +45,13 @@ class WhiteList extends Validator * * @param array $list * @param bool $strict disable type check and be case insensetive + * @param string $type of $list items */ - public function __construct(array $list, $strict = false) + public function __construct(array $list, $strict = false, $type = self::TYPE_STRING) { $this->list = $list; $this->strict = $strict; + $this->type = $type; if (!$this->strict) { foreach ($list as $key => &$value) { @@ -96,7 +103,7 @@ public function isArray(): bool */ public function getType() { - return self::TYPE_ARRAY; + return $this->type; } /** diff --git a/tests/Validator/WhiteListTest.php b/tests/Validator/WhiteListTest.php index 34f67779..c588a68c 100755 --- a/tests/Validator/WhiteListTest.php +++ b/tests/Validator/WhiteListTest.php @@ -29,7 +29,8 @@ public function testIsValid() $this->assertEquals($whiteList->isValid(3), true); $this->assertEquals($whiteList->isValid(5), false); $this->assertEquals($whiteList->getList(), ['string1', 'string2', 3, 4]); - $this->assertEquals($whiteList->getType(), 'array'); + $this->assertEquals($whiteList->getType(), 'string'); //string by default + $this->assertEquals($whiteList->isArray(), true); $whiteList = new WhiteList(['string1', 'string2', 3, 4], false); From 6e3edf8adf3f7867f11af604637264a5393f71fc Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 6 Apr 2021 08:09:41 -0400 Subject: [PATCH 22/29] Add missing assertions --- tests/Validator/ArrayListTest.php | 1 + tests/Validator/AssocTest.php | 1 + tests/Validator/BooleanTest.php | 1 + tests/Validator/FloatValidatorTest.php | 1 + tests/Validator/HexColorTest.php | 1 + tests/Validator/IntegerTest.php | 1 + tests/Validator/NumericTest.php | 2 ++ tests/Validator/RangeTest.php | 2 ++ tests/Validator/TextTest.php | 2 ++ 9 files changed, 12 insertions(+) diff --git a/tests/Validator/ArrayListTest.php b/tests/Validator/ArrayListTest.php index 386f5dec..90e927b4 100755 --- a/tests/Validator/ArrayListTest.php +++ b/tests/Validator/ArrayListTest.php @@ -52,5 +52,6 @@ public function testIsValid() $this->assertEquals(true, $this->arrayList2->isValid([1, 2, 3])); $this->assertEquals(false, $this->arrayList2->isValid(1, '2', 3)); $this->assertEquals($this->arrayList1->getType(), 'string'); + $this->assertEquals($this->arrayList1->isArray(), true); } } diff --git a/tests/Validator/AssocTest.php b/tests/Validator/AssocTest.php index 95a0c93e..4ca255e6 100755 --- a/tests/Validator/AssocTest.php +++ b/tests/Validator/AssocTest.php @@ -43,5 +43,6 @@ public function testIsValid() $this->assertEquals(true, $this->assoc->isValid(["a" => 'a', "b" => 'b', "c" => 'c'])); $this->assertEquals(true, $this->assoc->isValid([])); $this->assertEquals($this->assoc->getType(), 'array'); + $this->assertEquals($this->assoc->isArray(), true); } } diff --git a/tests/Validator/BooleanTest.php b/tests/Validator/BooleanTest.php index ca7cd962..1882b37f 100755 --- a/tests/Validator/BooleanTest.php +++ b/tests/Validator/BooleanTest.php @@ -61,5 +61,6 @@ public function testIsValid() $this->assertEquals(false, $this->boolean->isValid('string')); $this->assertEquals(false, $this->boolean->isValid(1.2)); $this->assertEquals($this->boolean->getType(), 'boolean'); + $this->assertEquals($this->boolean->isArray(), false); } } diff --git a/tests/Validator/FloatValidatorTest.php b/tests/Validator/FloatValidatorTest.php index fae035cd..502f179f 100755 --- a/tests/Validator/FloatValidatorTest.php +++ b/tests/Validator/FloatValidatorTest.php @@ -44,5 +44,6 @@ public function testIsValid() $this->assertEquals($this->validator->isValid('23.5'), false); $this->assertEquals($this->validator->isValid('23'), false); $this->assertEquals($this->validator->getType(), 'double'); + $this->assertEquals($this->validator->isArray(), false); } } diff --git a/tests/Validator/HexColorTest.php b/tests/Validator/HexColorTest.php index 73e3aa3d..f6e08ac7 100755 --- a/tests/Validator/HexColorTest.php +++ b/tests/Validator/HexColorTest.php @@ -45,5 +45,6 @@ public function testIsValid() $this->assertEquals($this->hexColor->isValid('fff'), true); $this->assertEquals($this->hexColor->isValid('000000'), true); $this->assertEquals($this->hexColor->getType(), 'string'); + $this->assertEquals($this->hexColor->isArray(), false); } } diff --git a/tests/Validator/IntegerTest.php b/tests/Validator/IntegerTest.php index 7baf8d48..dd56a8c1 100755 --- a/tests/Validator/IntegerTest.php +++ b/tests/Validator/IntegerTest.php @@ -43,5 +43,6 @@ public function testIsValid() $this->assertEquals($this->validator->isValid(true), false); $this->assertEquals($this->validator->isValid(false), false); $this->assertEquals($this->validator->getType(), 'integer'); + $this->assertEquals($this->validator->isArray(), false); } } diff --git a/tests/Validator/NumericTest.php b/tests/Validator/NumericTest.php index 56ea214f..a626268c 100755 --- a/tests/Validator/NumericTest.php +++ b/tests/Validator/NumericTest.php @@ -43,5 +43,7 @@ public function testIsValid() $this->assertEquals($this->numeric->isValid(9.1), true); $this->assertEquals($this->numeric->isValid('not numeric'), false); $this->assertEquals($this->numeric->isValid([]), false); + $this->assertEquals($this->numeric->getType(), 'mixed'); + $this->assertEquals($this->numeric->isArray(), false); } } diff --git a/tests/Validator/RangeTest.php b/tests/Validator/RangeTest.php index 098161ba..3ab420e9 100755 --- a/tests/Validator/RangeTest.php +++ b/tests/Validator/RangeTest.php @@ -44,5 +44,7 @@ public function testIsValid() $this->assertEquals($this->range->isValid(-1), false); $this->assertEquals($this->range->getMin(), 0); $this->assertEquals($this->range->getMax(), 5); + $this->assertEquals($this->range->isArray(), false); + $this->assertEquals($this->range->getType(), 'integer'); } } diff --git a/tests/Validator/TextTest.php b/tests/Validator/TextTest.php index 2b96b9e2..72f6b433 100755 --- a/tests/Validator/TextTest.php +++ b/tests/Validator/TextTest.php @@ -42,5 +42,7 @@ public function testIsValid() $this->assertEquals(false, $this->text->isValid(["seven"])); $this->assertEquals(false, $this->text->isValid(["seven", 8, 9.0])); $this->assertEquals(false, $this->text->isValid(false)); + $this->assertEquals(false, $this->text->isArray()); + $this->assertEquals('string', $this->text->getType()); } } From a783b5c1e4403764ba6c2431da7a5fda2133a3b1 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Thu, 8 Apr 2021 10:17:36 -0400 Subject: [PATCH 23/29] Add missing return type --- src/Validator/ArrayList.php | 2 +- src/Validator/Assoc.php | 2 +- src/Validator/Boolean.php | 2 +- src/Validator/FloatValidator.php | 2 +- src/Validator/HexColor.php | 2 +- src/Validator/Integer.php | 2 +- src/Validator/JSON.php | 2 +- src/Validator/Numeric.php | 2 +- src/Validator/Range.php | 2 +- src/Validator/Text.php | 2 +- src/Validator/WhiteList.php | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Validator/ArrayList.php b/src/Validator/ArrayList.php index f50f024c..34bc8239 100644 --- a/src/Validator/ArrayList.php +++ b/src/Validator/ArrayList.php @@ -71,7 +71,7 @@ public function isArray(): bool * * @return string */ - public function getType() + public function getType(): string { return $this->validator->getType(); } diff --git a/src/Validator/Assoc.php b/src/Validator/Assoc.php index 1ea4b521..de87e0c0 100644 --- a/src/Validator/Assoc.php +++ b/src/Validator/Assoc.php @@ -54,7 +54,7 @@ public function isArray(): bool * * @return string */ - public function getType() + public function getType(): string { return self::TYPE_ARRAY; } diff --git a/src/Validator/Boolean.php b/src/Validator/Boolean.php index 1b5d0006..ab1d6c2b 100644 --- a/src/Validator/Boolean.php +++ b/src/Validator/Boolean.php @@ -70,7 +70,7 @@ public function isArray(): bool * * @return string */ - public function getType() + public function getType(): string { return self::TYPE_BOOLEAN; } diff --git a/src/Validator/FloatValidator.php b/src/Validator/FloatValidator.php index d918b669..8244a86b 100755 --- a/src/Validator/FloatValidator.php +++ b/src/Validator/FloatValidator.php @@ -54,7 +54,7 @@ public function isArray(): bool * * @return string */ - public function getType() + public function getType(): string { return self::TYPE_FLOAT; } diff --git a/src/Validator/HexColor.php b/src/Validator/HexColor.php index 318c87ed..77c63990 100644 --- a/src/Validator/HexColor.php +++ b/src/Validator/HexColor.php @@ -43,7 +43,7 @@ public function isArray(): bool * * @return string */ - public function getType() + public function getType(): string { return self::TYPE_STRING; } diff --git a/src/Validator/Integer.php b/src/Validator/Integer.php index 9a8c08dc..01f4ad43 100755 --- a/src/Validator/Integer.php +++ b/src/Validator/Integer.php @@ -54,7 +54,7 @@ public function isArray(): bool * * @return string */ - public function getType() + public function getType(): string { return self::TYPE_INTEGER; } diff --git a/src/Validator/JSON.php b/src/Validator/JSON.php index 9bba0b70..05fdd279 100644 --- a/src/Validator/JSON.php +++ b/src/Validator/JSON.php @@ -43,7 +43,7 @@ public function isArray(): bool * * @return string */ - public function getType() + public function getType(): string { return self::TYPE_OBJECT; } diff --git a/src/Validator/Numeric.php b/src/Validator/Numeric.php index 96a60a86..53e7887a 100755 --- a/src/Validator/Numeric.php +++ b/src/Validator/Numeric.php @@ -54,7 +54,7 @@ public function isArray(): bool * * @return string */ - public function getType() + public function getType(): string { return self::TYPE_MIXED; } diff --git a/src/Validator/Range.php b/src/Validator/Range.php index d6b9c2c5..b946e651 100755 --- a/src/Validator/Range.php +++ b/src/Validator/Range.php @@ -90,7 +90,7 @@ public function isArray(): bool * * @return string */ - public function getType() + public function getType(): string { return self::TYPE_INTEGER; } diff --git a/src/Validator/Text.php b/src/Validator/Text.php index 58c6ee6f..72aed3a3 100644 --- a/src/Validator/Text.php +++ b/src/Validator/Text.php @@ -76,7 +76,7 @@ public function isArray(): bool * * @return string */ - public function getType() + public function getType(): string { return self::TYPE_STRING; } diff --git a/src/Validator/WhiteList.php b/src/Validator/WhiteList.php index 45df98a4..1d87eb1e 100755 --- a/src/Validator/WhiteList.php +++ b/src/Validator/WhiteList.php @@ -101,7 +101,7 @@ public function isArray(): bool * * @return string */ - public function getType() + public function getType(): string { return $this->type; } From 5f202ad069234f2858ae0f81ac64d257500d9847 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Thu, 8 Apr 2021 10:28:33 -0400 Subject: [PATCH 24/29] Revert changes for numeric deprecation --- src/Validator/Range.php | 2 +- tests/Validator/ArrayListTest.php | 2 +- tests/Validator/RangeTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Validator/Range.php b/src/Validator/Range.php index b946e651..e816e8af 100755 --- a/src/Validator/Range.php +++ b/src/Validator/Range.php @@ -19,7 +19,7 @@ * * @package Utopia\Validator */ -class Range extends Integer +class Range extends Numeric { /** * @var int diff --git a/tests/Validator/ArrayListTest.php b/tests/Validator/ArrayListTest.php index 90e927b4..a0643e5f 100755 --- a/tests/Validator/ArrayListTest.php +++ b/tests/Validator/ArrayListTest.php @@ -30,7 +30,7 @@ class ArrayListTest extends TestCase public function setUp():void { $this->arrayList1 = new ArrayList(new Text(100)); - $this->arrayList2 = new ArrayList(new Integer()); + $this->arrayList2 = new ArrayList(new Numeric()); } public function tearDown():void diff --git a/tests/Validator/RangeTest.php b/tests/Validator/RangeTest.php index 3ab420e9..e2a696f5 100755 --- a/tests/Validator/RangeTest.php +++ b/tests/Validator/RangeTest.php @@ -39,7 +39,7 @@ public function testIsValid() $this->assertEquals($this->range->isValid(1), true); $this->assertEquals($this->range->isValid(4), true); $this->assertEquals($this->range->isValid(5), true); - $this->assertEquals($this->range->isValid('5'), false); + $this->assertEquals($this->range->isValid('5'), true); $this->assertEquals($this->range->isValid(6), false); $this->assertEquals($this->range->isValid(-1), false); $this->assertEquals($this->range->getMin(), 0); From 8004864edaf845b0f1453c702e56bed00b2e03f2 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Thu, 8 Apr 2021 10:28:42 -0400 Subject: [PATCH 25/29] Use spaces to align declarations --- src/Validator/WhiteList.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Validator/WhiteList.php b/src/Validator/WhiteList.php index 1d87eb1e..0150e598 100755 --- a/src/Validator/WhiteList.php +++ b/src/Validator/WhiteList.php @@ -49,8 +49,8 @@ class WhiteList extends Validator */ public function __construct(array $list, $strict = false, $type = self::TYPE_STRING) { - $this->list = $list; - $this->strict = $strict; + $this->list = $list; + $this->strict = $strict; $this->type = $type; if (!$this->strict) { From e5ed42de796a8bd60e749ef438c3446d0b7eddda Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Thu, 8 Apr 2021 10:42:18 -0400 Subject: [PATCH 26/29] Add typed properties --- src/Validator/Boolean.php | 2 +- src/Validator/Range.php | 10 +++++----- src/Validator/Text.php | 2 +- src/Validator/WhiteList.php | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Validator/Boolean.php b/src/Validator/Boolean.php index ab1d6c2b..a275a1c0 100644 --- a/src/Validator/Boolean.php +++ b/src/Validator/Boolean.php @@ -26,7 +26,7 @@ class Boolean extends Validator /** * @var bool */ - protected $loose = false; + protected bool $loose = false; /** * Pass true to accept true and false strings and integers 0 and 1 as valid boolean values diff --git a/src/Validator/Range.php b/src/Validator/Range.php index e816e8af..e3c9033d 100755 --- a/src/Validator/Range.php +++ b/src/Validator/Range.php @@ -24,18 +24,18 @@ class Range extends Numeric /** * @var int */ - protected $min; + protected int $min; /** * @var int */ - protected $max; + protected int $max; /** * @param int $min * @param int $max */ - public function __construct($min, $max) + public function __construct(int $min, int $max) { $this->min = $min; $this->max = $max; @@ -45,7 +45,7 @@ public function __construct($min, $max) * Get Range Minimum Value * @return int */ - public function getMin() + public function getMin(): int { return $this->min; } @@ -54,7 +54,7 @@ public function getMin() * Get Range Maximum Value * @return int */ - public function getMax() + public function getMax(): int { return $this->max; } diff --git a/src/Validator/Text.php b/src/Validator/Text.php index 72aed3a3..439c20ad 100644 --- a/src/Validator/Text.php +++ b/src/Validator/Text.php @@ -26,7 +26,7 @@ class Text extends Validator /** * @var int */ - protected $length = 0; + protected int $length = 0; /** * Text constructor. diff --git a/src/Validator/WhiteList.php b/src/Validator/WhiteList.php index 0150e598..943ba8e4 100755 --- a/src/Validator/WhiteList.php +++ b/src/Validator/WhiteList.php @@ -26,17 +26,17 @@ class WhiteList extends Validator /** * @var array */ - protected $list; + protected array $list; /** * @var bool */ - protected $strict; + protected bool $strict; /** * @var string */ - protected $type; + protected string $type; /** * Constructor From 4861d27680d9937968986b87eb7c519d942154fc Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Thu, 8 Apr 2021 11:06:51 -0400 Subject: [PATCH 27/29] Check assertions against validator constants --- tests/Validator/ArrayListTest.php | 3 ++- tests/Validator/AssocTest.php | 2 +- tests/Validator/BooleanTest.php | 2 +- tests/Validator/FloatValidatorTest.php | 2 +- tests/Validator/HexColorTest.php | 2 +- tests/Validator/IntegerTest.php | 2 +- tests/Validator/JSONTest.php | 2 +- tests/Validator/NumericTest.php | 2 +- tests/Validator/RangeTest.php | 2 +- tests/Validator/TextTest.php | 2 +- tests/Validator/WhiteListTest.php | 2 +- 11 files changed, 12 insertions(+), 11 deletions(-) diff --git a/tests/Validator/ArrayListTest.php b/tests/Validator/ArrayListTest.php index a0643e5f..f3b56411 100755 --- a/tests/Validator/ArrayListTest.php +++ b/tests/Validator/ArrayListTest.php @@ -51,7 +51,8 @@ public function testIsValid() $this->assertEquals(false, $this->arrayList2->isValid('string')); $this->assertEquals(true, $this->arrayList2->isValid([1, 2, 3])); $this->assertEquals(false, $this->arrayList2->isValid(1, '2', 3)); - $this->assertEquals($this->arrayList1->getType(), 'string'); + $this->assertEquals($this->arrayList1->getType(), \Utopia\Validator::TYPE_STRING); + $this->assertEquals($this->arrayList2->getType(), \Utopia\Validator::TYPE_MIXED); $this->assertEquals($this->arrayList1->isArray(), true); } } diff --git a/tests/Validator/AssocTest.php b/tests/Validator/AssocTest.php index 4ca255e6..0c17c5ce 100755 --- a/tests/Validator/AssocTest.php +++ b/tests/Validator/AssocTest.php @@ -42,7 +42,7 @@ public function testIsValid() $this->assertEquals(true, $this->assoc->isValid(["1" => 'a', "0" => 'b', "2" => 'c'])); $this->assertEquals(true, $this->assoc->isValid(["a" => 'a', "b" => 'b', "c" => 'c'])); $this->assertEquals(true, $this->assoc->isValid([])); - $this->assertEquals($this->assoc->getType(), 'array'); + $this->assertEquals($this->assoc->getType(), \Utopia\Validator::TYPE_ARRAY); $this->assertEquals($this->assoc->isArray(), true); } } diff --git a/tests/Validator/BooleanTest.php b/tests/Validator/BooleanTest.php index 1882b37f..ed2a1027 100755 --- a/tests/Validator/BooleanTest.php +++ b/tests/Validator/BooleanTest.php @@ -60,7 +60,7 @@ public function testIsValid() $this->assertEquals(false, $this->boolean->isValid(['string', 'string'])); $this->assertEquals(false, $this->boolean->isValid('string')); $this->assertEquals(false, $this->boolean->isValid(1.2)); - $this->assertEquals($this->boolean->getType(), 'boolean'); + $this->assertEquals($this->boolean->getType(), \Utopia\Validator::TYPE_BOOLEAN); $this->assertEquals($this->boolean->isArray(), false); } } diff --git a/tests/Validator/FloatValidatorTest.php b/tests/Validator/FloatValidatorTest.php index 502f179f..817b1ab9 100755 --- a/tests/Validator/FloatValidatorTest.php +++ b/tests/Validator/FloatValidatorTest.php @@ -43,7 +43,7 @@ public function testIsValid() $this->assertEquals($this->validator->isValid(true), false); $this->assertEquals($this->validator->isValid('23.5'), false); $this->assertEquals($this->validator->isValid('23'), false); - $this->assertEquals($this->validator->getType(), 'double'); + $this->assertEquals($this->validator->getType(), \Utopia\Validator::TYPE_FLOAT); $this->assertEquals($this->validator->isArray(), false); } } diff --git a/tests/Validator/HexColorTest.php b/tests/Validator/HexColorTest.php index f6e08ac7..3a65e59a 100755 --- a/tests/Validator/HexColorTest.php +++ b/tests/Validator/HexColorTest.php @@ -44,7 +44,7 @@ public function testIsValid() $this->assertEquals($this->hexColor->isValid('ffffff'), true); $this->assertEquals($this->hexColor->isValid('fff'), true); $this->assertEquals($this->hexColor->isValid('000000'), true); - $this->assertEquals($this->hexColor->getType(), 'string'); + $this->assertEquals($this->hexColor->getType(), \Utopia\Validator::TYPE_STRING); $this->assertEquals($this->hexColor->isArray(), false); } } diff --git a/tests/Validator/IntegerTest.php b/tests/Validator/IntegerTest.php index dd56a8c1..efe68f3e 100755 --- a/tests/Validator/IntegerTest.php +++ b/tests/Validator/IntegerTest.php @@ -42,7 +42,7 @@ public function testIsValid() $this->assertEquals($this->validator->isValid(null), false); $this->assertEquals($this->validator->isValid(true), false); $this->assertEquals($this->validator->isValid(false), false); - $this->assertEquals($this->validator->getType(), 'integer'); + $this->assertEquals($this->validator->getType(), \Utopia\Validator::TYPE_INTEGER); $this->assertEquals($this->validator->isArray(), false); } } diff --git a/tests/Validator/JSONTest.php b/tests/Validator/JSONTest.php index c4692943..695bdf79 100755 --- a/tests/Validator/JSONTest.php +++ b/tests/Validator/JSONTest.php @@ -47,7 +47,7 @@ public function testIsValid() $this->assertEquals(true, $this->json->isValid(['test'])); $this->assertEquals(true, $this->json->isValid(['test' => 'demo'])); $this->assertEquals(true, $this->json->isValid('{"test": "demo"}')); - $this->assertEquals($this->json->getType(), 'object'); + $this->assertEquals($this->json->getType(), \Utopia\Validator::TYPE_OBJECT); $this->assertEquals($this->json->isArray(), false); } } diff --git a/tests/Validator/NumericTest.php b/tests/Validator/NumericTest.php index a626268c..948b0aff 100755 --- a/tests/Validator/NumericTest.php +++ b/tests/Validator/NumericTest.php @@ -43,7 +43,7 @@ public function testIsValid() $this->assertEquals($this->numeric->isValid(9.1), true); $this->assertEquals($this->numeric->isValid('not numeric'), false); $this->assertEquals($this->numeric->isValid([]), false); - $this->assertEquals($this->numeric->getType(), 'mixed'); + $this->assertEquals($this->numeric->getType(), \Utopia\Validator::TYPE_MIXED); $this->assertEquals($this->numeric->isArray(), false); } } diff --git a/tests/Validator/RangeTest.php b/tests/Validator/RangeTest.php index e2a696f5..88268b33 100755 --- a/tests/Validator/RangeTest.php +++ b/tests/Validator/RangeTest.php @@ -45,6 +45,6 @@ public function testIsValid() $this->assertEquals($this->range->getMin(), 0); $this->assertEquals($this->range->getMax(), 5); $this->assertEquals($this->range->isArray(), false); - $this->assertEquals($this->range->getType(), 'integer'); + $this->assertEquals($this->range->getType(), \Utopia\Validator::TYPE_INTEGER); } } diff --git a/tests/Validator/TextTest.php b/tests/Validator/TextTest.php index 72f6b433..f99dbaa8 100755 --- a/tests/Validator/TextTest.php +++ b/tests/Validator/TextTest.php @@ -43,6 +43,6 @@ public function testIsValid() $this->assertEquals(false, $this->text->isValid(["seven", 8, 9.0])); $this->assertEquals(false, $this->text->isValid(false)); $this->assertEquals(false, $this->text->isArray()); - $this->assertEquals('string', $this->text->getType()); + $this->assertEquals(\Utopia\Validator::TYPE_STRING, $this->text->getType()); } } diff --git a/tests/Validator/WhiteListTest.php b/tests/Validator/WhiteListTest.php index c588a68c..fcf2e97d 100755 --- a/tests/Validator/WhiteListTest.php +++ b/tests/Validator/WhiteListTest.php @@ -29,7 +29,7 @@ public function testIsValid() $this->assertEquals($whiteList->isValid(3), true); $this->assertEquals($whiteList->isValid(5), false); $this->assertEquals($whiteList->getList(), ['string1', 'string2', 3, 4]); - $this->assertEquals($whiteList->getType(), 'string'); //string by default + $this->assertEquals($whiteList->getType(), \Utopia\Validator::TYPE_STRING); //string by default $this->assertEquals($whiteList->isArray(), true); $whiteList = new WhiteList(['string1', 'string2', 3, 4], false); From d135f9ac2367c613780b648f936913fab42b8fa6 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Thu, 8 Apr 2021 11:12:20 -0400 Subject: [PATCH 28/29] Add property types to constructer function --- src/Validator/WhiteList.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Validator/WhiteList.php b/src/Validator/WhiteList.php index 943ba8e4..ade7ad09 100755 --- a/src/Validator/WhiteList.php +++ b/src/Validator/WhiteList.php @@ -47,7 +47,7 @@ class WhiteList extends Validator * @param bool $strict disable type check and be case insensetive * @param string $type of $list items */ - public function __construct(array $list, $strict = false, $type = self::TYPE_STRING) + public function __construct(array $list, bool $strict = false, string $type = self::TYPE_STRING) { $this->list = $list; $this->strict = $strict; From 6a17af8d056cc0dfccf02ccf4584be72c55ecced Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Thu, 8 Apr 2021 11:54:35 -0400 Subject: [PATCH 29/29] Revert "Add typed properties" This reverts commit e5ed42de796a8bd60e749ef438c3446d0b7eddda. --- src/Validator/Boolean.php | 2 +- src/Validator/Range.php | 10 +++++----- src/Validator/Text.php | 2 +- src/Validator/WhiteList.php | 6 +++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Validator/Boolean.php b/src/Validator/Boolean.php index a275a1c0..ab1d6c2b 100644 --- a/src/Validator/Boolean.php +++ b/src/Validator/Boolean.php @@ -26,7 +26,7 @@ class Boolean extends Validator /** * @var bool */ - protected bool $loose = false; + protected $loose = false; /** * Pass true to accept true and false strings and integers 0 and 1 as valid boolean values diff --git a/src/Validator/Range.php b/src/Validator/Range.php index e3c9033d..e816e8af 100755 --- a/src/Validator/Range.php +++ b/src/Validator/Range.php @@ -24,18 +24,18 @@ class Range extends Numeric /** * @var int */ - protected int $min; + protected $min; /** * @var int */ - protected int $max; + protected $max; /** * @param int $min * @param int $max */ - public function __construct(int $min, int $max) + public function __construct($min, $max) { $this->min = $min; $this->max = $max; @@ -45,7 +45,7 @@ public function __construct(int $min, int $max) * Get Range Minimum Value * @return int */ - public function getMin(): int + public function getMin() { return $this->min; } @@ -54,7 +54,7 @@ public function getMin(): int * Get Range Maximum Value * @return int */ - public function getMax(): int + public function getMax() { return $this->max; } diff --git a/src/Validator/Text.php b/src/Validator/Text.php index 439c20ad..72aed3a3 100644 --- a/src/Validator/Text.php +++ b/src/Validator/Text.php @@ -26,7 +26,7 @@ class Text extends Validator /** * @var int */ - protected int $length = 0; + protected $length = 0; /** * Text constructor. diff --git a/src/Validator/WhiteList.php b/src/Validator/WhiteList.php index ade7ad09..66cf22b0 100755 --- a/src/Validator/WhiteList.php +++ b/src/Validator/WhiteList.php @@ -26,17 +26,17 @@ class WhiteList extends Validator /** * @var array */ - protected array $list; + protected $list; /** * @var bool */ - protected bool $strict; + protected $strict; /** * @var string */ - protected string $type; + protected $type; /** * Constructor