Improvements and fixes on exceptions

There were some cases when the exceptions was not displayed property
when using nested exception, this commit fix the problem and also
display messages in Markdown list.
This commit is contained in:
Henrique Moody 2015-10-22 00:30:30 -02:00
parent fd6fcd151e
commit 8c9c587597
101 changed files with 564 additions and 325 deletions

View file

@ -51,6 +51,7 @@ All notable changes of the Respect\Validation releases are documented in this fi
- On exceptions, do not display parent message then rule has only one child (#407)
- On exceptions, improve `Object` conversion to string (#399)
- On exceptions, improve conversion of all values by using JSON (#399)
- On exceptions, nested messages are displayed in a Markdown list (#588)
- Rename rule "Arr" to "ArrayVal"
- Rename rule "Bool" to "BoolType" (#426)
- Rename rule "False" to "FalseVal" (#426)

View file

@ -1,6 +1,6 @@
# Feature Guide
## Namespace Import
## Namespace import
Respect\Validation is namespaced, but you can make your life easier by importing
a single class into your context:
@ -9,7 +9,7 @@ a single class into your context:
use Respect\Validation\Validator as v;
```
## Simple Validation
## Simple validation
The Hello World validator is something like this:
@ -18,17 +18,17 @@ $number = 123;
v::numeric()->validate($number); // true
```
## Chained Validation
## Chained validation
It is possible to use validators in a chain. Sample below validates a string
containing numbers and letters, no whitespace and length between 1 and 15.
```php
$usernameValidator = v::alnum()->noWhitespace()->length(1,15);
$usernameValidator = v::alnum()->noWhitespace()->length(1, 15);
$usernameValidator->validate('alganet'); // true
```
## Validating Object Attributes
## Validating object attributes
Given this simple object:
@ -76,7 +76,7 @@ By _optional_ we consider `null` or an empty string (`''`).
See more on [Optional](Optional.md).
## Negating Rules
## Negating rules
You can use the `v::not()` to negate any rule:
@ -84,7 +84,7 @@ You can use the `v::not()` to negate any rule:
v::not(v::intVal())->validate(10); // false, input must not be integer
```
## Validator Reuse
## Validator reuse
Once created, you can reuse your validator anywhere. Remember `$usernameValidator`?
@ -94,7 +94,7 @@ $usernameValidator->validate('alexandre gaigalas'); // false
$usernameValidator->validate('#$%'); //false
```
## Exception Types
## Exception types
* `Repect\Validation\Exceptions\ExceptionInterface`:
* All exceptions implement this interface;
@ -108,7 +108,7 @@ $usernameValidator->validate('#$%'); //false
* Use when calling `assert()`
* Interface has three methods: `getFullMessage()`, `findMessages()`, and `getMainMessage()`.
## Informative Exceptions
## Informative exceptions
When something goes wrong, Validation can tell you exactly what's going on. For this,
we use the `assert()` method instead of `validate()`:
@ -123,75 +123,80 @@ try {
}
```
The printed message is exactly this, as a text tree:
The printed message is exactly this, as a nested Markdown list:
```no-highlight
\-All of the 3 required rules must pass
|-"really messed up screen#name" must contain only letters (a-z) and digits (0-9)
|-"really messed up screen#name" must not contain whitespace
\-"really messed up screen#name" must have a length between 1 and 15
- All of the required rules must pass for "really messed up screen#name"
- "really messed up screen#name" must contain only letters (a-z) and digits (0-9)
- "really messed up screen#name" must not contain whitespace
- "really messed up screen#name" must have a length between 1 and 15
```
## Requesting Messages
## Getting all messages as an array
The text tree is fine, but unusable on a HTML form or something more custom. You can use
`findMessages()` for that:
The Markdown list is fine, but unusable on a HTML form or something more custom.
For that you can use `getMessages()`.
```php
use Respect\Validation\Exceptions\NestedValidationExceptionInterface;
try {
$usernameValidator->assert('really messed up screen#name');
} catch(NestedValidationExceptionInterface $exception) {
var_dump($exception->findMessages(['alnum', 'length', 'noWhitespace']));
}
```
`findMessages()` returns an array with messages from the requested validators.
## Getting Messages
Sometimes you just need all the messages, for that you can use `getMessages()`.
It will return all messages from the rules that did not pass the validation.
```php
try {
Validator::key('username', Validator::length(2, 32))
->key('birthdate', Validator::date())
->key('password', Validator::notEmpty())
->key('email', Validator::email())
->assert($input);
} catch (NestedValidationExceptionInterface $e) {
print_r($e->getMessages());
$usernameValidator->assert('really messed up screen#name');
} catch(NestedValidationExceptionInterface $exception) {
print_r($exception->getMessages());
}
```
The code above may display something like:
```
```no-highlight
Array
(
[0] => username must have a length between 2 and 32
[1] => birthdate must be a valid date
[2] => password must not be empty
[3] => Key email must be present
[0] => "really messed up screen#name" must contain only letters (a-z) and digits (0-9)
[1] => "really messed up screen#name" must not contain whitespace
[2] => "really messed up screen#name" must have a length between 1 and 15
)
```
## Custom Messages
## Getting messages as an array by name
If you want to get specific message by name you can use `findMessages()` passing
the names of the rules you want:
```php
try {
$usernameValidator->assert('really messed up screen#name');
} catch(NestedValidationExceptionInterface $exception) {
print_r($exception->findMessages(['alnum', 'noWhitespace']));
}
```
The `findMessages()` returns an array with messages from the requested validators,
like this:
```no-highlight
Array
(
[alnum] => "really messed up screen#name" must contain only letters (a-z) and digits (0-9)
[noWhitespace] => "really messed up screen#name" must not contain whitespace
)
```
## Custom messages
Getting messages as an array is fine, but sometimes you need to customize them in order
to present them to the user. This is possible using the `findMessages()` method as well:
```php
$errors = $exception->findMessages([
'alnum' => '{{name}} must contain only letters and digits',
'length' => '{{name}} must not have more than 15 chars',
'alnum' => '{{name}} must contain only letters and digits',
'length' => '{{name}} must not have more than 15 chars',
'noWhitespace' => '{{name}} cannot contain spaces'
]);
```
For all messages, the `{{name}}` and `{{input}}` variable is available for templates.
For all messages, the `{{name}}` variable is available for templates. If you
do not define a name it uses the input to replace this placeholder.
## Message localization
@ -211,7 +216,7 @@ the message will be translated.
Note that `getMessage()` will keep the original message.
## Custom Rules
## Custom rules
You also can use your own rules:
@ -245,7 +250,7 @@ v::with('My\\Validation\\Rules\\', true);
v::alnum(); // Try to use "My\Validation\Rules\Alnum" if any
```
## Validator Name
## Validator name
On `v::attribute()` and `v::key()`, `{{name}}` is the attribute/key name. For others,
is the same as the input. You can customize a validator name using:
@ -254,7 +259,7 @@ is the same as the input. You can customize a validator name using:
v::date('Y-m-d')->between('1980-02-02', 'now')->setName('Member Since');
```
## Zend/Symfony Validators
## Zend/Symfony validators
It is also possible to reuse validators from other frameworks if they are installed:
@ -263,7 +268,7 @@ $hostnameValidator = v::zend('Hostname')->assert('google.com');
$timeValidator = v::sf('Time')->assert('22:00:01');
```
## Validation Methods
## Validation methods
We've seen `validate()` that returns true or false and `assert()` that throws a complete
validation report. There is also a `check()` method that returns an Exception

View file

@ -1,57 +0,0 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
namespace Respect\Validation;
use RecursiveArrayIterator;
use Respect\Validation\Exceptions\AbstractNestedException;
class ExceptionIterator extends RecursiveArrayIterator
{
protected $fullRelated;
public function __construct($target, $fullRelated = false)
{
$this->fullRelated = $fullRelated;
parent::__construct(is_array($target) ? $target : [$target]);
}
public function current()
{
$current = parent::current();
if ($this->fullRelated
|| $current->hasCustomTemplate()
|| !$current instanceof AbstractNestedException) {
return $current;
}
$currentRelated = $current->getRelated(false);
if (count($currentRelated) == 1) {
return current($currentRelated);
}
return $current;
}
public function hasChildren()
{
if (!$this->current() instanceof AbstractNestedException) {
return false;
} else {
return (boolean) $this->current()->getRelated($this->fullRelated);
}
}
public function getChildren()
{
return new static($this->current()->getRelated($this->fullRelated), $this->fullRelated);
}
}

View file

@ -15,6 +15,7 @@ class AbstractGroupedException extends AbstractNestedException
{
const NONE = 0;
const SOME = 1;
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::NONE => 'All of the required rules must pass for {{name}}',
@ -29,32 +30,8 @@ class AbstractGroupedException extends AbstractNestedException
public function chooseTemplate()
{
$numRules = $this->getParam('passed');
$numFailed = count($this->getRelated());
$numFailed = $this->getRelated()->count();
return $numRules === $numFailed ? static::NONE : static::SOME;
}
public function getParams()
{
if (1 === count($this->related)) {
return current($this->related)->getParams();
} else {
return parent::getParams();
}
}
public function getTemplate()
{
$parentTemplate = parent::getTemplate();
$isEmpty = empty($this->template);
if (!$isEmpty && $this->template != $parentTemplate) {
return $this->template;
}
if ($isEmpty && 1 === count($this->related)) {
return current($this->related)->getTemplate();
} else {
return $parentTemplate;
}
}
}

View file

@ -12,23 +12,32 @@
namespace Respect\Validation\Exceptions;
use RecursiveIteratorIterator;
use RecursiveTreeIterator;
use Respect\Validation\ExceptionIterator;
use SplObjectStorage;
class AbstractNestedException extends ValidationException implements NestedValidationExceptionInterface
{
const ITERATE_TREE = 1;
const ITERATE_ALL = 2;
/**
* @var SplObjectStorage
*/
private $exceptions = [];
protected $related = [];
public function addRelated(ValidationException $related)
/**
* @param ValidationException $exception
*
* @return self
*/
public function addRelated(ValidationException $exception)
{
$this->related[spl_object_hash($related)] = $related;
$this->getRelated()->attach($exception);
return $this;
}
/**
* @param array $paths
*
* @return self
*/
public function findMessages(array $paths)
{
$messages = [];
@ -37,82 +46,162 @@ class AbstractNestedException extends ValidationException implements NestedValid
$numericKey = is_numeric($key);
$path = $numericKey ? $value : $key;
$e = $this->findRelated($path);
$exception = $this->findRelated($path);
if (is_object($e) && !$numericKey) {
$e->setTemplate($value);
if (is_object($exception) && !$numericKey) {
$exception->setTemplate($value);
}
$path = str_replace('.', '_', $path);
$messages[$path] = $e ? $e->getMainMessage() : '';
$messages[$path] = $exception ? $exception->getMainMessage() : '';
}
return $messages;
}
/**
* @return Exception
*/
public function findRelated($path)
{
$target = $this;
$path = explode('.', $path);
$pieces = explode('.', $path);
while (!empty($path) && $target !== false) {
$target = $target->getRelatedByName(array_shift($path));
while (!empty($pieces) && $target) {
$piece = array_shift($pieces);
$target = $target->getRelatedByName($piece);
}
return $target;
}
public function getIterator($full = false, $mode = self::ITERATE_ALL)
/**
* @return RecursiveIteratorIterator
*/
private function getRecursiveIterator()
{
$exceptionIterator = new ExceptionIterator($this, $full);
$exceptionIterator = new RecursiveExceptionIterator($this);
$recursiveIteratorIterator = new RecursiveIteratorIterator(
$exceptionIterator,
RecursiveIteratorIterator::SELF_FIRST
);
if ($mode == self::ITERATE_ALL) {
return new RecursiveIteratorIterator($exceptionIterator, 1);
} else {
return new RecursiveTreeIterator($exceptionIterator);
}
return $recursiveIteratorIterator;
}
public function getMessages()
/**
* @return SplObjectStorage
*/
public function getIterator()
{
$messages = [];
foreach ($this->getIterator() as $key => $exception) {
if ($key === 0) {
$childrenExceptions = new SplObjectStorage();
$recursiveIteratorIterator = $this->getRecursiveIterator();
$exceptionIterator = $recursiveIteratorIterator->getInnerIterator();
$lastDepth = 0;
$lastDepthOriginal = 0;
$knownDepths = [];
foreach ($recursiveIteratorIterator as $childException) {
if ($childException instanceof self
&& $childException->getRelated()->count() > 0
&& $childException->getRelated()->count() < 2) {
continue;
}
$currentDepth = $lastDepth;
$currentDepthOriginal = $recursiveIteratorIterator->getDepth() + 1;
if (isset($knownDepths[$currentDepthOriginal])) {
$currentDepth = $knownDepths[$currentDepthOriginal];
} elseif ($currentDepthOriginal > $lastDepthOriginal
&& ($this->hasCustomTemplate() || $exceptionIterator->count() != 1)) {
++$currentDepth;
}
if (!isset($knownDepths[$currentDepthOriginal])) {
$knownDepths[$currentDepthOriginal] = $currentDepth;
}
$lastDepth = $currentDepth;
$lastDepthOriginal = $currentDepthOriginal;
$childrenExceptions->attach(
$childException,
[
'depth' => $currentDepth,
'depth_original' => $currentDepthOriginal,
'previous_depth' => $lastDepth,
'previous_depth_original' => $lastDepthOriginal,
]
);
}
return $childrenExceptions;
}
/**
* @return array
*/
public function getMessages()
{
$messages = [$this->getMessage()];
foreach ($this as $exception) {
$messages[] = $exception->getMessage();
}
if (count($messages) > 1) {
array_shift($messages);
}
return $messages;
}
/**
* @return string
*/
public function getFullMessage()
{
$message = [];
$iterator = $this->getIterator(false, self::ITERATE_TREE);
foreach ($iterator as $m) {
$message[] = $m;
$marker = '-';
$messages = [];
$exceptions = $this->getIterator();
if ($this->hasCustomTemplate() || count($exceptions) != 1) {
$messages[] = sprintf('%s %s', $marker, $this->getMessage());
}
return implode(PHP_EOL, $message);
foreach ($exceptions as $exception) {
$depth = $exceptions[$exception]['depth'];
$prefix = str_repeat(' ', $depth * 2);
$messages[] = sprintf('%s%s %s', $prefix, $marker, $exception->getMessage());
}
return implode(PHP_EOL, $messages);
}
public function getRelated($full = false)
/**
* @return SplObjectStorage
*/
public function getRelated()
{
if (!$full && 1 === count($this->related)
&& current($this->related) instanceof self) {
return current($this->related)->getRelated();
} else {
return $this->related;
if (!$this->exceptions instanceof SplObjectStorage) {
$this->exceptions = new SplObjectStorage();
}
return $this->exceptions;
}
/**
* @param string $name
* @param mixed $value
*
* @return self
*/
public function setParam($name, $value)
{
if ('translator' === $name) {
foreach ($this->getRelated(true) as $related) {
$related->setParam($name, $value);
foreach ($this->getRelated() as $exception) {
$exception->setParam($name, $value);
}
}
@ -121,21 +210,39 @@ class AbstractNestedException extends ValidationException implements NestedValid
return $this;
}
public function getRelatedByName($name)
/**
* @return bool
*/
private function isRelated($name, ValidationException $exception)
{
foreach ($this->getIterator(true) as $e) {
if ($e->getId() === $name || $e->getName() === $name) {
return $e;
}
}
return false;
return ($exception->getId() === $name || $exception->getName() === $name);
}
public function setRelated(array $relatedExceptions)
/**
* @return ValidationException
*/
public function getRelatedByName($name)
{
foreach ($relatedExceptions as $related) {
$this->addRelated($related);
if ($this->isRelated($name, $this)) {
return $this;
}
foreach ($this->getRecursiveIterator() as $exception) {
if ($this->isRelated($name, $exception)) {
return $exception;
}
}
}
/**
* @param array $exceptions
*
* @return self
*/
public function setRelated(array $exceptions)
{
foreach ($exceptions as $exception) {
$this->addRelated($exception);
}
return $this;

View file

@ -11,7 +11,9 @@
namespace Respect\Validation\Exceptions;
interface NestedValidationExceptionInterface extends ValidationExceptionInterface
use IteratorAggregate;
interface NestedValidationExceptionInterface extends IteratorAggregate, ValidationExceptionInterface
{
public function findMessages(array $paths);
public function getMessages();

View file

@ -0,0 +1,69 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
namespace Respect\Validation\Exceptions;
use Countable;
use RecursiveIterator;
class RecursiveExceptionIterator implements RecursiveIterator, Countable
{
private $exceptions;
public function __construct(AbstractNestedException $parent)
{
$this->exceptions = $parent->getRelated();
}
public function count()
{
return $this->exceptions->count();
}
public function hasChildren()
{
if (!$this->valid()) {
return false;
}
return ($this->current() instanceof AbstractNestedException);
}
public function getChildren()
{
return new static($this->current());
}
public function current()
{
return $this->exceptions->current();
}
public function key()
{
return $this->exceptions->key();
}
public function next()
{
$this->exceptions->next();
}
public function rewind()
{
$this->exceptions->rewind();
}
public function valid()
{
return $this->exceptions->valid();
}
}

View file

@ -307,6 +307,8 @@ class ValidationException extends InvalidArgumentException implements Validation
$this->customTemplate = true;
$this->template = $template;
$this->buildMessage();
return $this;
}

View file

@ -12,6 +12,6 @@ try {
}
?>
--EXPECTF--
\-All of the required rules must pass for 42
|-42 must be a string
\-42 must contain only consonants
- All of the required rules must pass for 42
- 42 must be a string
- 42 must contain only consonants

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
\-"Frank Zappa is fantastic" must not be string
- "Frank Zappa is fantastic" must not be string

View file

@ -1,5 +1,5 @@
--FILE--
<?php
<?php
require 'vendor/autoload.php';
@ -11,9 +11,8 @@ try {
} catch (AllOfException $exception) {
echo $exception->getFullMessage();
}
?>
--EXPECTF--
\-All of the required rules must pass for "Bla %1#%&23"
|-"Bla %1#%&23" must contain only letters (a-z) and digits (0-9)
\-"Bla %1#%&23" must not contain whitespace
- All of the required rules must pass for "Bla %1#%&23"
- "Bla %1#%&23" must contain only letters (a-z) and digits (0-9)
- "Bla %1#%&23" must not contain whitespace

View file

@ -10,7 +10,6 @@ try {
} catch (AllOfException $exception) {
echo $exception->getFullMessage();
}
?>
--EXPECTF--
\-"asd124SF" must not contain letters (a-z) or digits (0-9)
- "asd124SF" must not contain letters (a-z) or digits (0-9)

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
1 must contain only letters (a-z)
\-2 must contain only letters (a-z)
- 2 must contain only letters (a-z)

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
Field must contain only letters (a-z)
\-Field must contain only letters (a-z)
- Field must contain only letters (a-z)

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
"a" must not contain letters (a-z)
\-"b" must not contain letters (a-z)
- "b" must not contain letters (a-z)

View file

@ -14,4 +14,4 @@ try {
}
?>
--EXPECTF--
\-"" is always invalid
- "" is always invalid

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
\-`[traversable] (ArrayObject: { })` must be of the type array
- `[traversable] (ArrayObject: { })` must be of the type array

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
\-{ 1, 2, 3 } must not be of the type array
- { 1, 2, 3 } must not be of the type array

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-`[object] (stdClass: { })` must be an array
- `[object] (stdClass: { })` must be an array

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-`[traversable] (ArrayObject: { 2, 3 })` must not be an array
- `[traversable] (ArrayObject: { 2, 3 })` must not be an array

View file

@ -40,14 +40,14 @@ try {
}
?>
--EXPECTF--
\-All of the required rules must pass for the given data
|-Key mysql must be valid
| |-host must be a string
| |-Key user must be present
| |-Key password must be present
| \-schema must be a string
\-Key postgresql must be valid
|-Key host must be present
|-user must be a string
|-password must be a string
\-Key schema must be present
- All of the required rules must pass for the given data
- All of the required rules must pass for mysql
- host must be a string
- Key user must be present
- Key password must be present
- schema must be a string
- All of the required rules must pass for postgresql
- Key host must be present
- user must be a string
- password must be a string
- Key schema must be present

View file

@ -11,4 +11,4 @@ try {
echo $e->getFullMessage();
}
--EXPECTF--
\-"c" must be lower than or equals "b"
- "c" must be lower than or equals "b"

View file

@ -11,4 +11,4 @@ try {
echo $e->getFullMessage();
}
--EXPECTF--
\-"a" must not be lower than or equals "b"
- "a" must not be lower than or equals "b"

View file

@ -11,4 +11,4 @@ try {
echo $e->getFullMessage();
}
--EXPECTF--
\-41 must not be lower than or equals 42
- 41 must not be lower than or equals 42

View file

@ -1,5 +1,5 @@
--FILE--
<?php
<?php
require 'vendor/autoload.php';
@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-"12345" must be a boolean
- "12345" must be a boolean

View file

@ -1,5 +1,5 @@
--FILE--
<?php
<?php
require 'vendor/autoload.php';
@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-true must not be a boolean
- true must not be a boolean

View file

@ -33,5 +33,5 @@ try {
--EXPECTF--
"ok" must be a boolean value
"yes" must not be a boolean value
\-"yep" must be a boolean value
\-"on" must not be a boolean value
- "yep" must be a boolean value
- "on" must not be a boolean value

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-"abc" must be a BSN
- "abc" must be a BSN

View file

@ -13,4 +13,4 @@ try
echo $e->getFullMessage();
}
--EXPECTF--
\-"test" must be a valid CNPJ number
- "test" must be a valid CNPJ number

View file

@ -14,4 +14,4 @@ try
}
--EXPECTF--
\-"65150175000120" must not be a valid CNPJ number
- "65150175000120" must not be a valid CNPJ number

View file

@ -14,4 +14,4 @@ try
}
--EXPECTF--
\-"65.150.175/0001-20" must contain only digits (0-9)
- "65.150.175/0001-20" must contain only digits (0-9)

View file

@ -11,4 +11,4 @@ try {
echo $e->getFullMessage();
}
--EXPECTF--
\-"Jaspion" must contain only consonants
- "Jaspion" must contain only consonants

View file

@ -11,4 +11,4 @@ try {
echo $e->getFullMessage();
}
--EXPECTF--
\-"bb" must not contain consonants
- "bb" must not contain consonants

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-"Not countable!" must be countable
- "Not countable!" must be countable

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-`[traversable] (ArrayObject: { })` must not be countable
- `[traversable] (ArrayObject: { })` must not be countable

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
\-"1" must be a valid country
- "1" must be a valid country

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
\-"BR" must not be a valid country
- "BR" must not be a valid country

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-"1" must be a valid country
- "1" must be a valid country

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-"BRA" must not be a valid country
- "BRA" must not be a valid country

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-"BRA" must be a valid country
- "BRA" must be a valid country

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-"076" must not be a valid country
- "076" must not be a valid country

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-"your mother" must be a valid CPF number
- "your mother" must be a valid CPF number

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-"368.928.062-10" must not be a valid CPF number
- "368.928.062-10" must not be a valid CPF number

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
\-"ppz" must be a valid currency
- "ppz" must be a valid currency

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
\-"GBP" must not be a valid currency
- "GBP" must not be a valid currency

View file

@ -12,4 +12,4 @@ try {
}
--EXPECTF--
\-"a" must contain only digits (0-9)
- "a" must contain only digits (0-9)

View file

@ -12,4 +12,4 @@ try {
}
--EXPECTF--
\-1 must not contain digits (0-9)
- 1 must not contain digits (0-9)

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
\-"bruce wayne" must be valid email
- "bruce wayne" must be valid email

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
\-"iambatman@gothancity.com" must not be an email
- "iambatman@gothancity.com" must not be an email

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
\-"test 1234" must be equals "test 123"
- "test 1234" must be equals "test 123"

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
\-"test 123" must not be equals "test 123"
- "test 123" must not be equals "test 123"

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
\-"1984.233" must be of the type float
- "1984.233" must be of the type float

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
\-1984.434 must not be of the type float
- 1984.434 must not be of the type float

View file

@ -14,6 +14,6 @@ try {
}
?>
--EXPECTF--
\-All of the required rules must pass for 0
|-0 must be a string
\-0 must have a length between 2 and 15
- All of the required rules must pass for 0
- 0 must be a string
- 0 must have a length between 2 and 15

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
\-"1984" must be of the type integer
- "1984" must be of the type integer

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
\-1984 must not be of the type integer
- 1984 must not be of the type integer

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
"foo" must be an IP address
\-"foo" must be an IP address
- "foo" must be an IP address

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
"10.0.0.1" must not be an IP address
\-"10.0.0.1" must not be an IP address
- "10.0.0.1" must not be an IP address

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
"127.0.0.1" must be an IP address in the "127.0.1.0-127.0.1.255" range
\-"127.0.0.1" must be an IP address in the "127.0.1.0-127.0.1.255" range
- "127.0.0.1" must be an IP address in the "127.0.1.0-127.0.1.255" range

View file

@ -0,0 +1,29 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Validator as v;
$config = [
'host' => 'my_host',
'password' => 'my_password',
'schema' => 'my_schema',
];
$validator = v::arrayType()
->setName('Settings')
->key('host', v::stringType())
->key('user', v::stringType())
->key('password', v::stringType())
->key('schema', v::stringType());
try {
$validator->assert($config);
} catch (AllOfException $exception) {
echo $exception->getFullMessage().PHP_EOL;
}
?>
--EXPECTF--
- Key user must be present

View file

@ -0,0 +1,29 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Validator as v;
use Respect\Validation\Exceptions\AllOfException;
$validator = v::create()
->key('age', v::intType()->notEmpty()->noneOf(v::stringType()))
->key('reference', v::stringType()->notEmpty()->length(1, 50));
try {
$validator->assert(['age' => 1]);
} catch (AllOfException $e) {
echo $e->getFullMessage();
}
echo PHP_EOL;
try {
$validator->assert(['reference' => 'QSF1234']);
} catch (AllOfException $e) {
echo $e->getFullMessage();
}
?>
--EXPECTF--
- Key reference must be present
- Key age must be present

View file

@ -0,0 +1,27 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Exceptions\NestedValidationExceptionInterface;
use Respect\Validation\Validator as v;
$arr = [
'name' => 'w',
'email' => 'hello@hello.com',
];
try {
v::create()
->key('name', v::length(2, 32))
->key('email', v::email())
->assert($arr);
} catch (NestedValidationExceptionInterface $e) {
print_r($e->getMessages());
}
?>
--EXPECTF--
Array
(
[0] => name must have a length between 2 and 32
)

View file

@ -13,4 +13,4 @@ try {
?>
--EXPECTF--
\-"String" must be iterable
- "String" must be iterable

View file

@ -13,4 +13,4 @@ try {
?>
--EXPECTF--
\-`[object] (stdClass: { })` must not be iterable
- `[object] (stdClass: { })` must not be iterable

View file

@ -16,4 +16,4 @@ try {
echo $e->getFullMessage();
}
--EXPECTF--
\-Key { "password": "123", "invalid_passwords": { "123", "secreta" } } must not be present
- Key { "password": "123", "invalid_passwords": { "123", "secreta" } } must not be present

View file

@ -20,6 +20,6 @@ try {
}
?>
--EXPECTF--
\-All of the required rules must pass for User Subscription Form
|-username must have a length between 2 and 32
\-birthdate must be a valid date
- All of the required rules must pass for User Subscription Form
- username must have a length between 2 and 32
- birthdate must be a valid date

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-"phpsp.org.br" must have a length between 3 and 5
- "phpsp.org.br" must have a length between 3 and 5

View file

@ -15,4 +15,4 @@ try {
?>
--EXPECTF--
\-The age must be 12 years or more.
- The age must be 12 years or more.

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
null must not be blank
\-"" must not be blank
- "" must not be blank

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
Field must not be blank
\-Field must not be blank
- Field must not be blank

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
1 must be blank
\-{ 1 } must be blank
- { 1 } must be blank

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
null must not be empty
\-"" must not be empty
- "" must not be empty

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
Field must not be empty
\-Field must not be empty
- Field must not be empty

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
1 must be empty
\-{ 1 } must be empty
- { 1 } must be empty

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
null must not be optional
\-"" must not be optional
- "" must not be optional

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
Field must not be optional
\-Field must not be optional
- Field must not be optional

View file

@ -26,5 +26,5 @@ try {
?>
--EXPECTF--
The value must be optional
\-{ } must be optional
\-Field must be optional
- { } must be optional
- Field must be optional

View file

@ -25,4 +25,4 @@ try {
}
?>
--EXPECTF--
\-These rules must not pass for 2
- These rules must not pass for 2

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
1 must be null
\-"" must be null
- "" must be null

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
Field must be null
\-Field must be null
- Field must be null

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
null must not be null
\-null must not be null
- null must not be null

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
"a" must be numeric
\-"a" must be numeric
- "a" must be numeric

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
Field must be numeric
\-Field must be numeric
- Field must be numeric

View file

@ -20,4 +20,4 @@ try {
?>
--EXPECTF--
"1" must not be numeric
\-"1" must not be numeric
- "1" must not be numeric

View file

@ -0,0 +1,16 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Validator as v;
$user = new stdClass;
$user->name = 'Alexandre';
$user->birthdate = '1987-07-01';
$userValidator = v::attribute('name', v::stringType()->length(1,32))
->attribute('birthdate', v::date()->age(18));
$userValidator->assert($user);
?>
--EXPECTF--

View file

@ -0,0 +1,19 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Validator as v;
use Respect\Validation\Exceptions\NestedValidationExceptionInterface;
$usernameValidator = v::alnum()->noWhitespace()->length(1, 15);
try {
$usernameValidator->assert('really messed up screen#name');
} catch(NestedValidationExceptionInterface $exception) {
echo $exception->getFullMessage();
}
?>
--EXPECTF--
- All of the required rules must pass for "really messed up screen#name"
- "really messed up screen#name" must contain only letters (a-z) and digits (0-9)
- "really messed up screen#name" must not contain whitespace
- "really messed up screen#name" must have a length between 1 and 15

View file

@ -0,0 +1,20 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Validator as v;
use Respect\Validation\Exceptions\NestedValidationExceptionInterface;
$usernameValidator = v::alnum()->noWhitespace()->length(1, 15);
try {
$usernameValidator->assert('really messed up screen#name');
} catch(NestedValidationExceptionInterface $exception) {
print_r($exception->findMessages(['alnum', 'noWhitespace']));
}
?>
--EXPECTF--
Array
(
[alnum] => "really messed up screen#name" must contain only letters (a-z) and digits (0-9)
[noWhitespace] => "really messed up screen#name" must not contain whitespace
)

View file

@ -0,0 +1,21 @@
--FILE--
<?php
require 'vendor/autoload.php';
use Respect\Validation\Validator as v;
use Respect\Validation\Exceptions\NestedValidationExceptionInterface;
$usernameValidator = v::alnum()->noWhitespace()->length(1, 15);
try {
$usernameValidator->assert('really messed up screen#name');
} catch(NestedValidationExceptionInterface $exception) {
print_r($exception->getMessages());
}
?>
--EXPECTF--
Array
(
[0] => "really messed up screen#name" must contain only letters (a-z) and digits (0-9)
[1] => "really messed up screen#name" must not contain whitespace
[2] => "really messed up screen#name" must have a length between 1 and 15
)

View file

@ -14,5 +14,5 @@ try {
}
?>
--EXPECTF--
\-"something" is not tasty
\-"something" must be greater than or equals 1
- "something" is not tasty
- "something" must be greater than or equals 1

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-"wrong slug" must be a valid slug
- "wrong slug" must be a valid slug

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-"good-and-valid-slug" must not be a valid slug
- "good-and-valid-slug" must not be a valid slug

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-42 must be a string
- 42 must be a string

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-"hello world" must not be string
- "hello world" must not be string

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
\-"1984" must be a valid top-level domain name
- "1984" must be a valid top-level domain name

View file

@ -12,4 +12,4 @@ try {
}
?>
--EXPECTF--
\-"com" must not be a valid top-level domain name
- "com" must not be a valid top-level domain name

View file

@ -25,6 +25,6 @@ try {
}
?>
--EXPECTF--
\-Todas as regras requeridas devem passar para 0
|-0 deve ser uma string
\-0 deve possuir de 2 a 15 caracteres
- Todas as regras requeridas devem passar para 0
- 0 deve ser uma string
- 0 deve possuir de 2 a 15 caracteres

View file

@ -16,4 +16,4 @@ try {
}
?>
--EXPECTF--
\-Data validation failed for 3
- Data validation failed for 3

View file

@ -13,4 +13,4 @@ try {
}
?>
--EXPECTF--
\-null is not considered as "Yes"
- null is not considered as "Yes"

View file

@ -1,26 +0,0 @@
<?php
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
namespace Respect\Validation\Exceptions;
class AbstractGroupedExceptionTest extends \PHPUnit_Framework_TestCase
{
public function testOneOrMoreGroupedExceptionsShouldBeCondensedByGetRelated()
{
$int = new IntValException();
$e = new AbstractGroupedException();
$e2 = new AbstractNestedException();
$e->addRelated($e2);
$e2->addRelated($int);
$result = $e->getRelated();
$this->assertSame($int, current($result));
}
}

View file

@ -66,7 +66,7 @@ class AbstractNestedExceptionTest extends \PHPUnit_Framework_TestCase
$this->assertSame($baz, $foo->findRelated('bar.baz'));
$this->assertSame($baz, $foo->findRelated('baz'));
$this->assertSame($bat, $foo->findRelated('bar.bat'));
$this->assertSame(false, $foo->findRelated('none'));
$this->assertSame(false, $foo->findRelated('bar.none'));
$this->assertNull($foo->findRelated('none'));
$this->assertNull($foo->findRelated('bar.none'));
}
}

Some files were not shown because too many files have changed in this diff Show more