From 4cfb93733b997138630c7c073a7dbc58d20ff147 Mon Sep 17 00:00:00 2001 From: Thomas Flori Date: Sun, 26 Nov 2017 11:51:23 +0100 Subject: [PATCH] update the documentation for error objects --- docs/index.md | 12 +++++--- docs/usage.md | 73 +++++++++++++++++++++++++++++++++----------- src/Error.php | 4 --- tests/ErrorsTest.php | 38 +++++++++++++++++++++++ 4 files changed, 100 insertions(+), 27 deletions(-) diff --git a/docs/index.md b/docs/index.md index cb4cd04..3fcb75c 100644 --- a/docs/index.md +++ b/docs/index.md @@ -5,12 +5,14 @@ permalink: /index.html --- ## {{ page.title }} -*Verja* is a very simple and stupid library to filter and validate input data. The name *Verja* comes from the Old -Norse language and means defender. The idea behind this name is that the library defends you from invalid input. +**Verja** is a very simple and stupid library to filter and validate input data. The name +[**Verja**](https://en.wiktionary.org/wiki/verja) (pronunciation **/ˈvɛrja/** +[IPA](https://en.wiktionary.org/wiki/Wiktionary:International_Phonetic_Alphabet)) comes from the Old Norse language and +means to defend. The idea behind this name is that the library defends you from invalid, missing and unwanted input. -The interface is very straight forward. `Verja\Gate` is the main object (you should **not reuse** this object). It -holds the data that should be validated, and the `Verja\Field`s. Each field has it's own filters and validators. When -you run `$container->validate()` each field gets filtered and validated. +The interface is very straight forward. `Verja\Gate` is the gate for your input data. It holds the data that should be +validated, and the `Verja\Field`s. Each field has it's own filters and validators. When you run `$container->validate()` +each field gets filtered and validated. Here is a small pseudo code example to explain the simplicity of this library: diff --git a/docs/usage.md b/docs/usage.md index dc2c13c..40524f4 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -172,28 +172,65 @@ all arrays of errors from the fields. The method `Gate::getErrors()` may return ```php?start_inline=true return [ 'foo' => [ - [ - 'key' => 'NOT_CONTAINS', - 'value' => 'any string', - 'message' => 'value should contain "bar"', - 'parameters' => [ 'subString' => 'bar' ], - ] + new \Verja\Error( + 'NOT_CONTAINS', + 'any string', + 'value should contain "bar"', + [ 'subString' => 'bar' ] + ) ], 'pw' => [ - [ - 'key' => 'STRLEN_TOO_SHORT', - 'value' => 'abc123', - 'message' => 'value should be at least 8 characters long', - 'parameters' => [ 'min' => 8, 'max' => 0 ], - ], - [ - 'key' => 'NOT_EQUAL', - 'value' => 'abc123', - 'message' => 'value should be equal to contexts pw_conf', - 'parameters' => [ 'opposite' => 'pw_conf', 'jsonEncode' => true ] - ] + new \Verja\Error( + 'STRLEN_TOO_SHORT', + 'abc123', + 'value should be at least 8 characters long', + [ 'min' => 8, 'max' => 0 ] + ), + new \Verja\Error( + 'NOT_EQUAL', + 'abc123', + 'value should be equal to contexts pw_conf', + [ 'opposite' => 'pw_conf', 'jsonEncode' => true ] + ) ], ]; +``` + +You can then serialize this data to this json: + +```json +{ + "foo": [ + { + "key": "NOT_CONTAINS", + "message": "value should contain \"bar\"", + "parameters": { + "subString": "bar", + "value": "any string" + } + } + ], + "pw": [ + { + "key": "STRLEN_TOO_SHORT", + "message": "value should be at least 8 characters long", + "parameters": { + "min": 8, + "max": 0, + "value": "abc123" + } + }, + { + "key": "NOT_EQUAL", + "message": "value should be equal to contexts pw_conf", + "parameters": { + "opposite": "pw_conf", + "jsonEncode": true, + "value": "abc123" + } + } + ] +} ``` ### Example diff --git a/src/Error.php b/src/Error.php index ce43d6a..54e7bac 100644 --- a/src/Error.php +++ b/src/Error.php @@ -7,9 +7,6 @@ class Error /** @var string */ public $key; - /** @var mixed */ - public $value; - /** @var string */ public $message; @@ -27,7 +24,6 @@ class Error public function __construct(string $key, $value, string $message = null, array $parameters = null) { $this->key = $key; - $this->value = $value; if ($message !== null) { $this->message = $message; diff --git a/tests/ErrorsTest.php b/tests/ErrorsTest.php index be0c7fa..35f5252 100644 --- a/tests/ErrorsTest.php +++ b/tests/ErrorsTest.php @@ -2,6 +2,7 @@ namespace Verja\Test; +use Verja\Error; use Verja\Field; use Verja\Gate; use Verja\Test\Examples\CustomValidator\GeneratedMessage; @@ -34,4 +35,41 @@ public function fieldCallsGetErrorWhenInvalid() $field->validate('value'); } + + /** @test */ + public function errorsCanBeSerialized() + { + $error = new Error('ERROR_KEY', 'validated value', 'Error message from validator'); + + $serialized = serialize($error); + + self::assertContains('ERROR_KEY', $serialized); + self::assertContains('validated value', $serialized); + self::assertContains('Error message from validator', $serialized); + } + + /** @test */ + public function errorsCanBeUnserialized() + { + $error = new Error('ERROR_KEY', 'validated value', 'Error message from validator'); + $serialized = serialize($error); + + $result = unserialize($serialized); + + self::assertEquals($error, $result); + } + + /** @test */ + public function errorsCanBeJsonEncoded() + { + $error = new Error('ERROR_KEY', 'validated value', 'Error message from validator'); + + $json = json_encode($error); + + self::assertSame(json_encode([ + 'key' => 'ERROR_KEY', + 'message' => 'Error message from validator', + 'parameters' => ['value' => 'validated value'], + ]), $json); + } }