Skip to content

Commit

Permalink
update the documentation for error objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Flori committed Nov 26, 2017
1 parent e8f62b5 commit 4cfb937
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 27 deletions.
12 changes: 7 additions & 5 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
73 changes: 55 additions & 18 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ class Error
/** @var string */
public $key;

/** @var mixed */
public $value;

/** @var string */
public $message;

Expand All @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions tests/ErrorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Verja\Test;

use Verja\Error;
use Verja\Field;
use Verja\Gate;
use Verja\Test\Examples\CustomValidator\GeneratedMessage;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 4cfb937

Please sign in to comment.