From 24b14154e3586cc7f2d5f51e8d05f0f3f0c2bac6 Mon Sep 17 00:00:00 2001 From: Franck Date: Fri, 27 Mar 2020 11:45:56 -0400 Subject: [PATCH 01/12] updated --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0e4e5cd..05c3249 100644 --- a/README.md +++ b/README.md @@ -151,5 +151,7 @@ interface ValidationInterface public function expectKeysToBeString(array $keys, bool $acceptNull = false); public function expectKeyToBeBoolean(string $key, bool $acceptNull = false); public function expectKeysToBeBoolean(array $keys, bool $acceptNull = false); + public function expectKeyToBeObject(string $key, bool $acceptNull = false); + public function expectKeysToBeObject(array $keys, bool $acceptNull = false); } ``` \ No newline at end of file From 6612599f6fa358ba7a5fb100320c279c9ee79770 Mon Sep 17 00:00:00 2001 From: Franck Date: Fri, 27 Mar 2020 11:56:01 -0400 Subject: [PATCH 02/12] updated --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 05c3249..78d492e 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ you can afterward check the value of those keys with your business logic without ```php $validator = new Validator(); -if ($validator->expectExactlyKeys($array, $keys) === true) { +if ($validator->expectExactlyKeys($data, $keys) === true) { // ... } ``` @@ -47,7 +47,8 @@ $validation ->expectKeyToBeString('name'); if ($validation->hasErrors()) { - // die($validation->getLastError()) + // $lastError = $validation->getLastError(); + // $errors = $validation->getErrors(); } ``` From 88052d62c5aed21d3b259318b9283f879bcb42fb Mon Sep 17 00:00:00 2001 From: Franck Date: Fri, 27 Mar 2020 12:56:28 -0400 Subject: [PATCH 03/12] updated --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 78d492e..1232d3d 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ if ($validation->hasErrors()) { ```php -$validation = new StrictValidation($arrayToValidate); +$validation = new StrictValidation($data); // will throw an exception if any of tests below fail $validation From 3a040764f9128bda924e9767cb73a2d834d16f3c Mon Sep 17 00:00:00 2001 From: Franck Date: Fri, 27 Mar 2020 12:57:25 -0400 Subject: [PATCH 04/12] updated --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1232d3d..e6ae589 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ if ($validation->hasErrors()) { ## 5- Create a validation Schema for later usage -Schema is just another way to write validation definitions. This format is ideal when you want to store schemas in file (ex: json, php array file, yml, ...) +Schema is just another way to write validation definitions. This format is ideal when you want to store schemas in file (ex: json, php array file, yml, etc.) ```php From d158af55ebc59cd97cb8aa817f9b08cc0d2766a6 Mon Sep 17 00:00:00 2001 From: Franck Date: Fri, 27 Mar 2020 12:58:44 -0400 Subject: [PATCH 05/12] updated --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e6ae589..cf01ece 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ This component help you to validate array structure by: - ensuring a data structure with expected keys requirements - preventing structure pollution by allowing only a set of keys -This is specially useful when dealing with json data request, before using the data, you must validate his content so +This is especially useful when dealing with json data request, before using the data, you must validate his content so you can afterward check the value of those keys with your business logic without worrying about the type or presence of any key value. # How to use From 5cab8a8749f2699771e1eb5f9784203f4427f1bb Mon Sep 17 00:00:00 2001 From: Matias Ibargoyen Date: Fri, 27 Mar 2020 22:15:24 +0000 Subject: [PATCH 06/12] added missing extension "ext-json" to composer.json --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c734826..ca57287 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "type": "library", "require": { "php": ">=7.2", - "symfony/polyfill-php73": "^1.14" + "symfony/polyfill-php73": "^1.14", + "ext-json": "*" }, "require-dev": { "phpunit/phpunit": "^9.0", From b4dc6f309997b752a21bc7e6653c0b5435ced67c Mon Sep 17 00:00:00 2001 From: Matias Ibargoyen Date: Fri, 27 Mar 2020 22:16:45 +0000 Subject: [PATCH 07/12] added "autoload-dev" to composer.json --- composer.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/composer.json b/composer.json index ca57287..7eae421 100644 --- a/composer.json +++ b/composer.json @@ -17,5 +17,10 @@ "psr-4": { "Peak\\ArrayValidation\\": "src/" } + }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } } } From 3da255ea538b1677186f8cb9e05064a6b146d579 Mon Sep 17 00:00:00 2001 From: Matias Ibargoyen Date: Fri, 27 Mar 2020 22:17:45 +0000 Subject: [PATCH 08/12] added 2 missing namespaces, and also use statements --- tests/StrictValidationTest.php | 2 ++ tests/ValidationTest.php | 5 +++-- tests/ValidatorTest.php | 4 ++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/StrictValidationTest.php b/tests/StrictValidationTest.php index 70d83a2..6287cd3 100644 --- a/tests/StrictValidationTest.php +++ b/tests/StrictValidationTest.php @@ -2,6 +2,8 @@ declare(strict_types=1); +namespace Tests; + use Peak\ArrayValidation\Exception\InvalidStructureException; use Peak\ArrayValidation\Exception\InvalidTypeException; use Peak\ArrayValidation\StrictValidation; diff --git a/tests/ValidationTest.php b/tests/ValidationTest.php index 59d5956..6dd1c05 100644 --- a/tests/ValidationTest.php +++ b/tests/ValidationTest.php @@ -6,6 +6,7 @@ use Peak\ArrayValidation\Validation; use \PHPUnit\Framework\TestCase; +use StdClass; class ValidationTest extends TestCase { @@ -15,7 +16,7 @@ public function dataProvider() return [ [ - ['field1' => [], 'field2' => 'text', 'field3' => 1, 'field4'=> true, 'field5' => 1.2, 'field6' => new \StdClass()], // data + ['field1' => [], 'field2' => 'text', 'field3' => 1, 'field4'=> true, 'field5' => 1.2, 'field6' => new StdClass()], // data null, // dataName [ // validation rules 'expectOnlyKeys' => [ ['field1', 'field2', 'field3', 'field4', 'field5', 'field6'] ], @@ -41,7 +42,7 @@ public function dataProvider() // this one will test types errors [ - ['field6' => [], 'field5' => 'text', 'field4' => 1, 'field3'=> true, 'field2' => 1.2, 'field1' => new \StdClass()], // data + ['field6' => [], 'field5' => 'text', 'field4' => 1, 'field3'=> true, 'field2' => 1.2, 'field1' => new StdClass()], // data 'myValidation', // dataName [ // validation rules 'expectKeyToBeArray' => ['field1'], diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 9c41921..6d79dba 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -2,8 +2,12 @@ declare(strict_types=1); +namespace Tests; + +use DateTime; use \PHPUnit\Framework\TestCase; use \Peak\ArrayValidation\Validator; +use stdClass; class ValidatorTest extends TestCase { From 68a289eb0b661b347b3bd950e0d5990f1dbc8fb7 Mon Sep 17 00:00:00 2001 From: Franck Date: Fri, 27 Mar 2020 19:28:41 -0400 Subject: [PATCH 09/12] updated --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f13169..9cf72ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +VERSION 3.1.1 +------------- +Release date: 2020-03-27 + + - added "autoload-dev" to composer.json (thank you @ordago) + - added missing extension "ext-json" to composer.json (thank you @ordago) + VERSION 3.1.0 ------------- Release date: 2020-03-26 From ec02b520d7cadadfaae9e19449a42f868c2d2934 Mon Sep 17 00:00:00 2001 From: Franck Date: Thu, 2 Apr 2020 12:33:44 -0400 Subject: [PATCH 10/12] added --- src/ValidationBuilder.php | 56 +++++++++++++++++++++++++ tests/ValidationBuilderTest.php | 72 +++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 src/ValidationBuilder.php create mode 100644 tests/ValidationBuilderTest.php diff --git a/src/ValidationBuilder.php b/src/ValidationBuilder.php new file mode 100644 index 0000000..0b77d75 --- /dev/null +++ b/src/ValidationBuilder.php @@ -0,0 +1,56 @@ +errors = $validation->getErrors(); + $this->lastError = $validation->getLastError(); + return !$validation->hasErrors(); + } + + /** + * @param array $data + * @throw InvalidStructureException + * @throw InvalidTypeException + */ + public function strictValidate(array $data): void + { + new StrictValidationFromDefinition($this, $data); + } + + /** + * @return array + */ + public function getErrors(): array + { + return $this->errors; + } + + /** + * @return string|null + */ + public function getLastError(): ?string + { + return $this->lastError; + } +} diff --git a/tests/ValidationBuilderTest.php b/tests/ValidationBuilderTest.php new file mode 100644 index 0000000..abcbdf9 --- /dev/null +++ b/tests/ValidationBuilderTest.php @@ -0,0 +1,72 @@ +expectNKeys(2) + ->expectAtLeastKeys(['title', 'content']) + ->expectExactlyKeys(['title', 'content']) + ->expectOnlyKeys(['title', 'content']) + ->expectKeysToBeString(['title', 'content'], true); + + $data = [ + 'title' => 'test', + 'content' => 'test', + ]; + + $validationPass = $validation->validate($data); + $this->assertTrue($validationPass); + $this->assertTrue($validation->getErrors() === []); + $this->assertTrue($validation->getLastError() === null); + } + + public function testStrictValidate() + { + $validation = new ValidationBuilder(); + $validation + ->expectNKeys(2) + ->expectAtLeastKeys(['title', 'content']) + ->expectExactlyKeys(['title', 'content']) + ->expectOnlyKeys(['title', 'content']) + ->expectKeysToBeString(['title', 'content'], true); + + $data = [ + 'title' => 'test', + 'content' => 'test', + ]; + + $validation->strictValidate($data); + $this->assertTrue(true); + } + + public function testStrictValidateException() + { + $validation = new ValidationBuilder(); + $validation + ->expectNKeys(2) + ->expectAtLeastKeys(['title', 'content']) + ->expectExactlyKeys(['title', 'content']) + ->expectOnlyKeys(['title', 'content']) + ->expectKeysToBeString(['title', 'content'], true); + + $data = [ + 'title' => 1, + 'content' => 'test', + ]; + + $this->expectException(ArrayValidationExceptionInterface::class); + $validation->strictValidate($data); + } + +} From 7584822297a0b9809f187823ad5a0aef16546526 Mon Sep 17 00:00:00 2001 From: Franck Date: Thu, 2 Apr 2020 12:33:51 -0400 Subject: [PATCH 11/12] updated --- CHANGELOG.md | 6 ++++++ README.md | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9cf72ed..65b07f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +VERSION 3.2.0 +------------- +Release date: 2020-04-02 + + - added ValidationBuilder + VERSION 3.1.1 ------------- Release date: 2020-03-27 diff --git a/README.md b/README.md index cf01ece..42ac3f7 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,28 @@ new StrictValidationFromDefinition($validationDefinition, $arrayToValidate); new StrictValidationFromSchema($schema, $arrayToValidate); ``` +## 8- Validation and Strict Validation with ValidationBuilder + + +```php +$validation = new ValidationBuilder(); +$validation + ->expectOnlyKeys(['title', 'content', 'description']) + ->expectAtLeastKeys(['title', 'content']) + ->expectKeysToBeString(['title', 'content', 'description']); + +if ($validation->validate($data) === false) { + // $validation->getErrors(); + // $validation->getLastError(); +} +``` + +and with strict validation: + +```php +// will throw an exception if any of tests fail +$validation->strictValidate($data); +``` # Validation methods ```php From 8ee7ea80474183d551c5c355b643304deea770ec Mon Sep 17 00:00:00 2001 From: Franck Date: Thu, 2 Apr 2020 12:35:13 -0400 Subject: [PATCH 12/12] updated --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 42ac3f7..f06b3e5 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ This is especially useful when dealing with json data request, before using the you can afterward check the value of those keys with your business logic without worrying about the type or presence of any key value. # How to use -##### 7 Usages +##### 8 Usages ## 1- General validation "à la carte" (stateless) @@ -133,7 +133,6 @@ new StrictValidationFromSchema($schema, $arrayToValidate); ## 8- Validation and Strict Validation with ValidationBuilder - ```php $validation = new ValidationBuilder(); $validation