diff --git a/README.md b/README.md
index 6731ac5..9ff9756 100644
--- a/README.md
+++ b/README.md
@@ -34,27 +34,19 @@ use Respect\Validation\Validator as v;
$app = new \Slim\App();
-// Fetch DI Container
-$container = $app->getContainer();
-
-// Register provider
-$container['apiValidation'] = function () {
- //Create the validators
- $usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
- $ageValidator = v::numeric()->positive()->between(1, 20);
- $validators = array(
- 'username' => $usernameValidator,
- 'age' => $ageValidator
- );
-
- return new \DavidePastore\Slim\Validation\Validation($validators);
-};
+//Create the validators
+$usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
+$ageValidator = v::numeric()->positive()->between(1, 20);
+$validators = array(
+ 'username' => $usernameValidator,
+ 'age' => $ageValidator
+);
$app->get('/api/myEndPoint',function ($req, $res, $args) {
//Here you expect 'username' and 'age' parameters
- if($this->apiValidation->hasErrors()){
+ if($req->getAttribute('has_errors')){
//There are errors, read them
- $errors = $this->apiValidation->getErrors();
+ $errors = $req->getAttribute('errors');
/* $errors contain:
array(
@@ -70,7 +62,7 @@ $app->get('/api/myEndPoint',function ($req, $res, $args) {
//No errors
}
-})->add($container->get('apiValidation'));
+})->add(new \DavidePastore\Slim\Validation\Validation($validators));
$app->run();
```
@@ -83,31 +75,23 @@ use Respect\Validation\Validator as v;
$app = new \Slim\App();
-// Fetch DI Container
-$container = $app->getContainer();
-
-// Register provider
-$container['validation'] = function () {
- //Create the validators
- $usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
- $ageValidator = v::numeric()->positive()->between(1, 20);
- $validators = array(
- 'username' => $usernameValidator,
- 'age' => $ageValidator
- );
-
- return new \DavidePastore\Slim\Validation\Validation($validators);
-};
+//Create the validators
+$usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
+$ageValidator = v::numeric()->positive()->between(1, 20);
+$validators = array(
+ 'username' => $usernameValidator,
+ 'age' => $ageValidator
+);
// Register middleware for all routes
// If you are implementing per-route checks you must not add this
-$app->add($container->get('validation'));
+$app->add(return new \DavidePastore\Slim\Validation\Validation($validators));
$app->get('/foo', function ($req, $res, $args) {
//Here you expect 'username' and 'age' parameters
- if($this->validation->hasErrors()){
+ if($req->getAttribute('has_errors')){
//There are errors, read them
- $errors = $this->validation->getErrors();
+ $errors = $req->getAttribute('errors');
/* $errors contain:
array(
@@ -126,9 +110,9 @@ $app->get('/foo', function ($req, $res, $args) {
$app->post('/bar', function ($req, $res, $args) {
//Here you expect 'username' and 'age' parameters
- if($this->validation->hasErrors()){
+ if($req->getAttribute('has_errors')){
//There are errors, read them
- $errors = $this->validation->getErrors();
+ $errors = $req->getAttribute('errors');
} else {
//No errors
}
@@ -162,28 +146,22 @@ use Respect\Validation\Validator as v;
$app = new \Slim\App();
-// Fetch DI Container
-$container = $app->getContainer();
-$container['apiValidation'] = function () {
- //Create the validators
- $typeValidator = v::alnum()->noWhitespace()->length(3, 5);
- $emailNameValidator = v::alnum()->noWhitespace()->length(1, 2);
- $validators = array(
- 'type' => $typeValidator,
- 'email' => array(
- 'name' => $emailNameValidator,
- ),
- );
-
- return new \DavidePastore\Slim\Validation\Validation($validators);
-};
+//Create the validators
+$typeValidator = v::alnum()->noWhitespace()->length(3, 5);
+$emailNameValidator = v::alnum()->noWhitespace()->length(1, 2);
+$validators = array(
+ 'type' => $typeValidator,
+ 'email' => array(
+ 'name' => $emailNameValidator,
+ ),
+);
```
If you'll have an error, the result would be:
```php
//In your route
-$errors = $this->apiValidation->getErrors();
+$errors = $req->getAttribute('errors');
print_r($errors);
/*
@@ -225,21 +203,15 @@ use Respect\Validation\Validator as v;
$app = new \Slim\App();
-// Fetch DI Container
-$container = $app->getContainer();
-$container['apiValidation'] = function () {
- //Create the validators
- $typeValidator = v::alnum()->noWhitespace()->length(3, 5);
- $emailNameValidator = v::alnum()->noWhitespace()->length(1, 2);
- $validators = array(
- 'type' => $typeValidator,
- 'email' => array(
- 'name' => $emailNameValidator,
- ),
- );
-
- return new \DavidePastore\Slim\Validation\Validation($validators);
-};
+//Create the validators
+$typeValidator = v::alnum()->noWhitespace()->length(3, 5);
+$emailNameValidator = v::alnum()->noWhitespace()->length(1, 2);
+$validators = array(
+ 'type' => $typeValidator,
+ 'email' => array(
+ 'name' => $emailNameValidator,
+ ),
+);
```
@@ -247,7 +219,7 @@ If you'll have an error, the result would be:
```php
//In your route
-$errors = $this->apiValidation->getErrors();
+$errors = $req->getAttribute('errors');
print_r($errors);
/*
@@ -272,31 +244,25 @@ use Respect\Validation\Validator as v;
$app = new \Slim\App();
-// Fetch DI Container
-$container = $app->getContainer();
-
-// Register provider
-$container['validation'] = function () {
- //Create the validators
- $usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
- $ageValidator = v::numeric()->positive()->between(1, 20);
- $validators = array(
- 'username' => $usernameValidator,
- 'age' => $ageValidator
- );
-
- $translator = function($message){
- $messages = [
- 'These rules must pass for {{name}}' => 'Queste regole devono passare per {{name}}',
- '{{name}} must be a string' => '{{name}} deve essere una stringa',
- '{{name}} must have a length between {{minValue}} and {{maxValue}}' => '{{name}} deve avere una dimensione di caratteri compresa tra {{minValue}} e {{maxValue}}',
- ];
- return $messages[$message];
- };
-
- return new \DavidePastore\Slim\Validation\Validation($validators, $translator);
+//Create the validators
+$usernameValidator = v::alnum()->noWhitespace()->length(1, 10);
+$ageValidator = v::numeric()->positive()->between(1, 20);
+$validators = array(
+ 'username' => $usernameValidator,
+ 'age' => $ageValidator
+);
+
+$translator = function($message){
+ $messages = [
+ 'These rules must pass for {{name}}' => 'Queste regole devono passare per {{name}}',
+ '{{name}} must be a string' => '{{name}} deve essere una stringa',
+ '{{name}} must have a length between {{minValue}} and {{maxValue}}' => '{{name}} deve avere una dimensione di caratteri compresa tra {{minValue}} e {{maxValue}}',
+ ];
+ return $messages[$message];
};
+$middleware = new \DavidePastore\Slim\Validation\Validation($validators, $translator);
+
// Register middleware for all routes or only for one...
$app->run();
diff --git a/src/Validation.php b/src/Validation.php
index 4b9da86..8d45148 100644
--- a/src/Validation.php
+++ b/src/Validation.php
@@ -30,6 +30,34 @@ class Validation
*/
protected $errors = [];
+ /**
+ * The 'errors' attribute name.
+ *
+ * @var string
+ */
+ protected $errors_name = 'errors';
+
+ /**
+ * The 'has_error' attribute name.
+ *
+ * @var string
+ */
+ protected $has_errors_name = 'has_errors';
+
+ /**
+ * The 'validators' attribute name.
+ *
+ * @var string
+ */
+ protected $validators_name = 'validators';
+
+ /**
+ * The 'translator' attribute name.
+ *
+ * @var string
+ */
+ protected $translator_name = 'translator';
+
/**
* Create new Validator service provider.
*
@@ -62,6 +90,11 @@ public function __invoke($request, $response, $next)
$params = $request->getParams();
$this->validate($params, $this->validators);
+ $request = $request->withAttribute($this->errors_name, $this->getErrors());
+ $request = $request->withAttribute($this->has_errors_name, $this->hasErrors());
+ $request = $request->withAttribute($this->validators_name, $this->getValidators());
+ $request = $request->withAttribute($this->translator_name, $this->getTranslator());
+
return $next($request, $response);
}
diff --git a/tests/ValidationTest.php b/tests/ValidationTest.php
index 1697da2..633173d 100644
--- a/tests/ValidationTest.php
+++ b/tests/ValidationTest.php
@@ -97,38 +97,50 @@ public function testValidationWithoutErrors()
);
$mw = new Validation($validators);
- $next = function ($req, $res) {
+ $hasErrors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$hasErrors, &$validators) {
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
+
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $this->assertFalse($mw->hasErrors());
- $this->assertEquals($validators, $mw->getValidators());
+ $this->assertFalse($hasErrors);
+ $this->assertEquals($validators, $validators);
}
public function testValidationWithErrors()
{
$usernameValidator = v::alnum()->noWhitespace()->length(1, 5);
- $validators = array(
+ $expectedValidators = array(
'username' => $usernameValidator,
);
- $mw = new Validation($validators);
+ $mw = new Validation($expectedValidators);
+
+ $errors = null;
+ $hasErrors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$errors, &$hasErrors, &$validators) {
+ $errors = $req->getAttribute('errors');
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
- $next = function ($req, $res) {
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $this->assertTrue($mw->hasErrors());
- $errors = array(
+ $this->assertTrue($hasErrors);
+ $expectedErrors = array(
'username' => array(
'"davidepastore" must have a length between 1 and 5',
),
);
- $this->assertEquals($errors, $mw->getErrors());
- $this->assertEquals($validators, $mw->getValidators());
+ $this->assertEquals($expectedErrors, $errors);
+ $this->assertEquals($expectedValidators, $validators);
}
public function testValidationNotExistingOptionalParameter()
@@ -139,15 +151,22 @@ public function testValidationNotExistingOptionalParameter()
);
$mw = new Validation($validators);
- $next = function ($req, $res) {
+ $errors = null;
+ $hasErrors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$errors, &$hasErrors, &$validators) {
+ $errors = $req->getAttribute('errors');
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
+
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $errors = array();
- $this->assertFalse($mw->hasErrors());
- $this->assertEquals($errors, $mw->getErrors());
+ $expectedErrors = array();
+ $this->assertFalse($hasErrors);
+ $this->assertEquals($expectedErrors, $errors);
}
public function testValidationNotExistingParameter()
@@ -158,76 +177,104 @@ public function testValidationNotExistingParameter()
);
$mw = new Validation($validators);
- $next = function ($req, $res) {
+ $errors = null;
+ $hasErrors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$errors, &$hasErrors, &$validators) {
+ $errors = $req->getAttribute('errors');
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
+
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $errors = array(
+ $expectedErrors = array(
'notExisting' => array(
'null must contain only letters (a-z)',
),
);
- $this->assertTrue($mw->hasErrors());
- $this->assertEquals($errors, $mw->getErrors());
+ $this->assertTrue($hasErrors);
+ $this->assertEquals($expectedErrors, $errors);
}
public function testValidationWithoutValidators()
{
$mw = new Validation();
- $next = function ($req, $res) {
+ $errors = null;
+ $hasErrors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$errors, &$hasErrors, &$validators) {
+ $errors = $req->getAttribute('errors');
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
+
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $errors = array();
- $validators = [];
- $this->assertFalse($mw->hasErrors());
- $this->assertEquals($errors, $mw->getErrors());
- $this->assertEquals($validators, $mw->getValidators());
+ $expectedErrors = array();
+ $expectedValidators = [];
+ $this->assertFalse($hasErrors);
+ $this->assertEquals($expectedErrors, $errors);
+ $this->assertEquals($expectedValidators, $validators);
}
public function testMultipleValidationWithoutErrors()
{
$usernameValidator = v::alnum()->noWhitespace()->length(1, 20);
$ageValidator = v::numeric()->positive()->between(1, 100);
- $validators = array(
+ $expectedValidators = array(
'username' => $usernameValidator,
'age' => $ageValidator,
);
- $mw = new Validation($validators);
+ $mw = new Validation($expectedValidators);
+
+ $errors = null;
+ $hasErrors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$errors, &$hasErrors, &$validators) {
+ $errors = $req->getAttribute('errors');
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
- $next = function ($req, $res) {
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $this->assertFalse($mw->hasErrors());
- $this->assertEquals(array(), $mw->getErrors());
- $this->assertEquals($validators, $mw->getValidators());
+ $this->assertFalse($hasErrors);
+ $this->assertEquals(array(), $errors);
+ $this->assertEquals($expectedValidators, $validators);
}
public function testMultipleValidationWithErrors()
{
$usernameValidator = v::alnum()->noWhitespace()->length(1, 5);
$ageValidator = v::numeric()->positive()->between(1, 60);
- $validators = array(
+ $expectedValidators = array(
'username' => $usernameValidator,
'age' => $ageValidator,
);
- $mw = new Validation($validators);
+ $mw = new Validation($expectedValidators);
+
+ $errors = null;
+ $hasErrors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$errors, &$hasErrors, &$validators) {
+ $errors = $req->getAttribute('errors');
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
- $next = function ($req, $res) {
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $this->assertTrue($mw->hasErrors());
- $errors = array(
+ $this->assertTrue($hasErrors);
+ $expectedErrors = array(
'username' => array(
'"davidepastore" must have a length between 1 and 5',
),
@@ -235,21 +282,28 @@ public function testMultipleValidationWithErrors()
'"89" must be lower than or equals 60',
),
);
- $this->assertEquals($errors, $mw->getErrors());
- $this->assertEquals($validators, $mw->getValidators());
+ $this->assertEquals($expectedErrors, $errors);
+ $this->assertEquals($expectedValidators, $validators);
}
public function testSetValidators()
{
$usernameValidator = v::alnum()->noWhitespace()->length(1, 20);
$ageValidator = v::numeric()->positive()->between(1, 100);
- $validators = array(
+ $expectedValidators = array(
'username' => $usernameValidator,
'age' => $ageValidator,
);
- $mw = new Validation($validators);
+ $mw = new Validation($expectedValidators);
+
+ $errors = null;
+ $hasErrors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$errors, &$hasErrors, &$validators) {
+ $errors = $req->getAttribute('errors');
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
- $next = function ($req, $res) {
return $res;
};
@@ -264,7 +318,7 @@ public function testSetValidators()
$response = $mw($this->request, $this->response, $next);
- $errors = array(
+ $expectedErrors = array(
'username' => array(
'"davidepastore" must have a length between 1 and 10',
),
@@ -273,19 +327,19 @@ public function testSetValidators()
),
);
- $this->assertTrue($mw->hasErrors());
- $this->assertEquals($errors, $mw->getErrors());
- $this->assertEquals($newValidators, $mw->getValidators());
+ $this->assertTrue($hasErrors);
+ $this->assertEquals($expectedErrors, $errors);
+ $this->assertEquals($newValidators, $validators);
}
public function testValidationWithCallableTranslator()
{
$usernameValidator = v::alnum()->noWhitespace()->length(1, 5);
- $validators = array(
+ $expectedValidators = array(
'username' => $usernameValidator,
);
- $translator = function ($message) {
+ $expectedTranslator = function ($message) {
$messages = [
'These rules must pass for {{name}}' => 'Queste regole devono passare per {{name}}',
'{{name}} must be a string' => '{{name}} deve essere una stringa',
@@ -295,29 +349,38 @@ public function testValidationWithCallableTranslator()
return $messages[$message];
};
- $mw = new Validation($validators, $translator);
+ $mw = new Validation($expectedValidators, $expectedTranslator);
+
+ $errors = null;
+ $hasErrors = null;
+ $translator = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$errors, &$hasErrors, &$translator, &$validators) {
+ $errors = $req->getAttribute('errors');
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
+ $translator = $req->getAttribute('translator');
- $next = function ($req, $res) {
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $this->assertTrue($mw->hasErrors());
- $errors = array(
+ $this->assertTrue($hasErrors);
+ $expectedErrors = array(
'username' => array(
'"davidepastore" deve avere una dimensione di caratteri compresa tra 1 e 5',
),
);
- $this->assertEquals($errors, $mw->getErrors());
- $this->assertEquals($validators, $mw->getValidators());
- $this->assertEquals($translator, $mw->getTranslator());
+ $this->assertEquals($expectedErrors, $errors);
+ $this->assertEquals($expectedValidators, $validators);
+ $this->assertEquals($expectedTranslator, $translator);
}
public function testSetTranslator()
{
$usernameValidator = v::alnum()->noWhitespace()->length(1, 5);
- $validators = array(
+ $expectedValidators = array(
'username' => $usernameValidator,
);
@@ -331,9 +394,18 @@ public function testSetTranslator()
return $messages[$message];
};
- $mw = new Validation($validators, $translator);
+ $mw = new Validation($expectedValidators, $translator);
+
+ $errors = null;
+ $hasErrors = null;
+ $translator = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$errors, &$hasErrors, &$translator, &$validators) {
+ $errors = $req->getAttribute('errors');
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
+ $translator = $req->getAttribute('translator');
- $next = function ($req, $res) {
return $res;
};
@@ -351,15 +423,15 @@ public function testSetTranslator()
$response = $mw($this->request, $this->response, $next);
- $this->assertTrue($mw->hasErrors());
- $errors = array(
+ $this->assertTrue($hasErrors);
+ $expectedErrors = array(
'username' => array(
'"davidepastore" deve avere una dimensione di caratteri compresa tra 1 e 5 (nuovo)',
),
);
- $this->assertEquals($errors, $mw->getErrors());
- $this->assertEquals($validators, $mw->getValidators());
- $this->assertEquals($newTranslator, $mw->getTranslator());
+ $this->assertEquals($expectedErrors, $errors);
+ $this->assertEquals($expectedValidators, $validators);
+ $this->assertEquals($newTranslator, $translator);
}
public function testJsonValidationWithoutErrors()
@@ -369,19 +441,24 @@ public function testJsonValidationWithoutErrors()
);
$this->setUpPost($json);
$usernameValidator = v::alnum()->noWhitespace()->length(1, 15);
- $validators = array(
+ $expectedValidators = array(
'username' => $usernameValidator,
);
- $mw = new Validation($validators);
+ $mw = new Validation($expectedValidators);
+
+ $hasErrors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$hasErrors, &$validators) {
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
- $next = function ($req, $res) {
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $this->assertFalse($mw->hasErrors());
- $this->assertEquals($validators, $mw->getValidators());
+ $this->assertFalse($hasErrors);
+ $this->assertEquals($expectedValidators, $validators);
}
public function testJsonValidationWithErrors()
@@ -391,25 +468,32 @@ public function testJsonValidationWithErrors()
);
$this->setUpPost($json);
$usernameValidator = v::alnum()->noWhitespace()->length(1, 5);
- $validators = array(
+ $expectedValidators = array(
'username' => $usernameValidator,
);
- $mw = new Validation($validators);
+ $mw = new Validation($expectedValidators);
+
+ $errors = null;
+ $hasErrors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$errors, &$hasErrors, &$validators) {
+ $errors = $req->getAttribute('errors');
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
- $next = function ($req, $res) {
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $errors = array(
+ $expectedErrors = array(
'username' => array(
'"jsonusername" must have a length between 1 and 5',
),
);
- $this->assertEquals($errors, $mw->getErrors());
- $this->assertEquals($validators, $mw->getValidators());
+ $this->assertEquals($expectedErrors, $errors);
+ $this->assertEquals($expectedValidators, $validators);
}
public function testComplexJsonValidationWithoutErrors()
@@ -429,23 +513,30 @@ public function testComplexJsonValidationWithoutErrors()
$typeValidator = v::alnum()->noWhitespace()->length(3, 8);
$emailNameValidator = v::alnum()->noWhitespace()->length(1, 5);
$emailIdValidator = v::numeric()->positive()->between(1, 20);
- $validators = array(
+ $expectedValidators = array(
'type' => $typeValidator,
'email' => array(
'id' => $emailIdValidator,
'name' => $emailNameValidator,
),
);
- $mw = new Validation($validators);
+ $mw = new Validation($expectedValidators);
+
+ $errors = null;
+ $hasErrors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$errors, &$hasErrors, &$validators) {
+ $errors = $req->getAttribute('errors');
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
- $next = function ($req, $res) {
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $this->assertFalse($mw->hasErrors());
- $this->assertEquals($validators, $mw->getValidators());
+ $this->assertFalse($hasErrors);
+ $this->assertEquals($expectedValidators, $validators);
}
public function testComplexJsonValidationWithErrors()
@@ -464,21 +555,28 @@ public function testComplexJsonValidationWithErrors()
$this->setUpPost($json);
$typeValidator = v::alnum()->noWhitespace()->length(3, 5);
$emailNameValidator = v::alnum()->noWhitespace()->length(1, 2);
- $validators = array(
+ $expectedValidators = array(
'type' => $typeValidator,
'email' => array(
'name' => $emailNameValidator,
),
);
- $mw = new Validation($validators);
+ $mw = new Validation($expectedValidators);
+
+ $errors = null;
+ $hasErrors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$errors, &$hasErrors, &$validators) {
+ $errors = $req->getAttribute('errors');
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
- $next = function ($req, $res) {
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $errors = array(
+ $expectedErrors = array(
'type' => array(
'"emails" must have a length between 3 and 5',
),
@@ -487,8 +585,8 @@ public function testComplexJsonValidationWithErrors()
),
);
- $this->assertEquals($errors, $mw->getErrors());
- $this->assertEquals($validators, $mw->getValidators());
+ $this->assertEquals($expectedErrors, $errors);
+ $this->assertEquals($expectedValidators, $validators);
}
public function testMoreComplexJsonValidationWithoutErrors()
@@ -507,7 +605,7 @@ public function testMoreComplexJsonValidationWithoutErrors()
);
$this->setUpPost($json);
$finallyValidator = v::numeric()->positive()->between(1, 200);
- $validators = array(
+ $expectedValidators = array(
'email' => array(
'sub' => array(
'sub-sub' => array(
@@ -516,35 +614,40 @@ public function testMoreComplexJsonValidationWithoutErrors()
),
),
);
- $mw = new Validation($validators);
+ $mw = new Validation($expectedValidators);
+
+ $hasErrors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$hasErrors, &$validators) {
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
- $next = function ($req, $res) {
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $this->assertFalse($mw->hasErrors());
- $this->assertEquals($validators, $mw->getValidators());
+ $this->assertFalse($hasErrors);
+ $this->assertEquals($expectedValidators, $validators);
}
public function testMoreComplexJsonValidationWithErrors()
{
$json = array(
- 'finally' => 22,
- 'email' => array(
- 'finally' => 33,
- 'sub' => array(
- 'finally' => 97,
- 'sub-sub' => array(
- 'finally' => 321,
+ 'finally' => 22,
+ 'email' => array(
+ 'finally' => 33,
+ 'sub' => array(
+ 'finally' => 97,
+ 'sub-sub' => array(
+ 'finally' => 321,
+ ),
),
),
- ),
- );
+ );
$this->setUpPost($json);
$finallyValidator = v::numeric()->positive()->between(1, 200);
- $validators = array(
+ $expectedValidators = array(
'email' => array(
'sub' => array(
'sub-sub' => array(
@@ -553,22 +656,27 @@ public function testMoreComplexJsonValidationWithErrors()
),
),
);
- $mw = new Validation($validators);
+ $mw = new Validation($expectedValidators);
+
+ $errors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$errors, &$validators) {
+ $errors = $req->getAttribute('errors');
+ $validators = $req->getAttribute('validators');
- $next = function ($req, $res) {
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $errors = array(
+ $expectedErrors = array(
'email.sub.sub-sub.finally' => array(
'321 must be lower than or equals 200',
),
);
- $this->assertEquals($errors, $mw->getErrors());
- $this->assertEquals($validators, $mw->getValidators());
+ $this->assertEquals($expectedErrors, $errors);
+ $this->assertEquals($expectedValidators, $validators);
}
public function testXmlValidationWithoutErrors()
@@ -576,19 +684,24 @@ public function testXmlValidationWithoutErrors()
$xml = 'Josh';
$this->setUpXmlPost($xml);
$nameValidator = v::alnum()->noWhitespace()->length(1, 15);
- $validators = array(
+ $expectedValidators = array(
'name' => $nameValidator,
);
- $mw = new Validation($validators);
+ $mw = new Validation($expectedValidators);
+
+ $hasErrors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$hasErrors, &$validators) {
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
- $next = function ($req, $res) {
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $this->assertFalse($mw->hasErrors());
- $this->assertEquals($validators, $mw->getValidators());
+ $this->assertFalse($hasErrors);
+ $this->assertEquals($expectedValidators, $validators);
}
public function testXmlValidationWithErrors()
@@ -596,25 +709,30 @@ public function testXmlValidationWithErrors()
$xml = 'jsonusername';
$this->setUpXmlPost($xml);
$nameValidator = v::alnum()->noWhitespace()->length(1, 5);
- $validators = array(
+ $expectedValidators = array(
'name' => $nameValidator,
);
- $mw = new Validation($validators);
+ $mw = new Validation($expectedValidators);
+
+ $errors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$errors, &$validators) {
+ $errors = $req->getAttribute('errors');
+ $validators = $req->getAttribute('validators');
- $next = function ($req, $res) {
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $errors = array(
+ $expectedErrors = array(
'name' => array(
'"jsonusername" must have a length between 1 and 5',
),
);
- $this->assertEquals($errors, $mw->getErrors());
- $this->assertEquals($validators, $mw->getValidators());
+ $this->assertEquals($expectedErrors, $errors);
+ $this->assertEquals($expectedValidators, $validators);
}
public function testComplexXmlValidationWithoutErrors()
@@ -634,23 +752,28 @@ public function testComplexXmlValidationWithoutErrors()
$typeValidator = v::alnum()->noWhitespace()->length(3, 8);
$emailNameValidator = v::alnum()->noWhitespace()->length(1, 5);
$emailIdValidator = v::numeric()->positive()->between(1, 20);
- $validators = array(
+ $expectedValidators = array(
'type' => $typeValidator,
'email' => array(
'id' => $emailIdValidator,
'name' => $emailNameValidator,
),
);
- $mw = new Validation($validators);
+ $mw = new Validation($expectedValidators);
+
+ $hasErrors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$hasErrors, &$validators) {
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
- $next = function ($req, $res) {
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $this->assertFalse($mw->hasErrors());
- $this->assertEquals($validators, $mw->getValidators());
+ $this->assertFalse($hasErrors);
+ $this->assertEquals($expectedValidators, $validators);
}
public function testComplexXmlValidationWithErrors()
@@ -669,21 +792,26 @@ public function testComplexXmlValidationWithErrors()
$this->setUpXmlPost($xml);
$typeValidator = v::alnum()->noWhitespace()->length(3, 5);
$emailNameValidator = v::alnum()->noWhitespace()->length(1, 2);
- $validators = array(
+ $expectedValidators = array(
'type' => $typeValidator,
'email' => array(
'name' => $emailNameValidator,
),
);
- $mw = new Validation($validators);
+ $mw = new Validation($expectedValidators);
+
+ $errors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$errors, &$validators) {
+ $errors = $req->getAttribute('errors');
+ $validators = $req->getAttribute('validators');
- $next = function ($req, $res) {
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $errors = array(
+ $expectedErrors = array(
'type' => array(
'"emails" must have a length between 3 and 5',
),
@@ -692,8 +820,8 @@ public function testComplexXmlValidationWithErrors()
),
);
- $this->assertEquals($errors, $mw->getErrors());
- $this->assertEquals($validators, $mw->getValidators());
+ $this->assertEquals($expectedErrors, $errors);
+ $this->assertEquals($expectedValidators, $validators);
}
public function testMoreComplexXmlValidationWithoutErrors()
@@ -712,7 +840,7 @@ public function testMoreComplexXmlValidationWithoutErrors()
';
$this->setUpXmlPost($xml);
$finallyValidator = v::numeric()->positive()->between(1, 200);
- $validators = array(
+ $expectedValidators = array(
'email' => array(
'sub' => array(
'sub-sub' => array(
@@ -721,16 +849,21 @@ public function testMoreComplexXmlValidationWithoutErrors()
),
),
);
- $mw = new Validation($validators);
+ $mw = new Validation($expectedValidators);
+
+ $hasErrors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$hasErrors, &$validators) {
+ $hasErrors = $req->getAttribute('has_errors');
+ $validators = $req->getAttribute('validators');
- $next = function ($req, $res) {
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $this->assertFalse($mw->hasErrors());
- $this->assertEquals($validators, $mw->getValidators());
+ $this->assertFalse($hasErrors);
+ $this->assertEquals($expectedValidators, $validators);
}
public function testMoreComplexXmlValidationWithErrors()
@@ -749,7 +882,7 @@ public function testMoreComplexXmlValidationWithErrors()
';
$this->setUpXmlPost($xml);
$finallyValidator = v::numeric()->positive()->between(1, 200);
- $validators = array(
+ $expectedValidators = array(
'email' => array(
'sub' => array(
'sub-sub' => array(
@@ -758,21 +891,26 @@ public function testMoreComplexXmlValidationWithErrors()
),
),
);
- $mw = new Validation($validators);
+ $mw = new Validation($expectedValidators);
+
+ $errors = null;
+ $validators = [];
+ $next = function ($req, $res) use (&$errors, &$validators) {
+ $errors = $req->getAttribute('errors');
+ $validators = $req->getAttribute('validators');
- $next = function ($req, $res) {
return $res;
};
$response = $mw($this->request, $this->response, $next);
- $errors = array(
+ $expectedErrors = array(
'email.sub.sub-sub.finally' => array(
'"321" must be lower than or equals 200',
),
);
- $this->assertEquals($errors, $mw->getErrors());
- $this->assertEquals($validators, $mw->getValidators());
+ $this->assertEquals($expectedErrors, $errors);
+ $this->assertEquals($expectedValidators, $validators);
}
}