Skip to content

Commit c27b40a

Browse files
committed
Improve Validator, passing property name now, and use property name in exception message
1 parent 98d3812 commit c27b40a

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

src/Middleware/ValidatorMiddleware.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,30 @@
1313
use Swoft\Http\Server\Validator\HttpValidator;
1414

1515
/**
16-
* validator of request
17-
*
16+
* Validator middleware
1817
* @Bean()
19-
* @uses ValidatorMiddleware
20-
* @version 2017年12月03日
21-
* @author stelin <[email protected]>
22-
* @copyright Copyright 2010-2016 swoft software
23-
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
2418
*/
2519
class ValidatorMiddleware implements MiddlewareInterface
2620
{
2721
/**
28-
* do process
29-
*
3022
* @param \Psr\Http\Message\ServerRequestInterface $request
3123
* @param \Psr\Http\Server\RequestHandlerInterface $handler
32-
*
3324
* @return \Psr\Http\Message\ResponseInterface
3425
* @throws \Swoft\Exception\ValidatorException
3526
*/
3627
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
3728
{
3829
$httpHandler = $request->getAttribute(AttributeEnum::ROUTER_ATTRIBUTE);
39-
$info = $httpHandler[2];
30+
$info = $httpHandler[2];
4031

4132
if (isset($info['handler']) && \is_string($info['handler'])) {
42-
$exploded = explode('@', $info['handler']);
43-
$className = $exploded[0] ?? '';
33+
$exploded = explode('@', $info['handler']);
34+
$className = $exploded[0] ?? '';
4435
$validatorKey = $exploded[1] ?? '';
45-
$matches = $info['matches']??[];
36+
$matches = $info['matches'] ?? [];
4637

4738
/* @var HttpValidator $validator */
48-
$validator = App::getBean(HttpValidator::class);
39+
$validator = App::getBean(HttpValidator::class);
4940
$collector = ValidatorCollector::getCollector();
5041

5142
if (isset($collector[$className][$validatorKey]['validator'])) {

src/Validator/HttpValidator.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@
1414
class HttpValidator extends AbstractValidator
1515
{
1616
/**
17-
* validate
17+
* Validate
1818
*
19-
* @param mixed $validators
2019
* @param array ...$params
2120
* @return mixed
2221
* @throws \Swoft\Exception\ValidatorException
2322
*/
24-
public function validate($validators, ...$params)
23+
public function validate(...$params)
2524
{
2625
/**
2726
* @var Request $request
2827
* @var array $matches
2928
*/
30-
list($request, $matches) = $params;
29+
list($validators, $request, $matches) = $params;
3130

3231
if (! \is_array($validators)) {
3332
return $request;
@@ -61,7 +60,7 @@ private function validateField($request, array $matches, string $type, array $va
6160
$request = $request->addQueryParam($name, $default);
6261
continue;
6362
}
64-
$this->doValidation($get[$name], $info);
63+
$this->doValidation($name, $get[$name], $info);
6564

6665
continue;
6766
}
@@ -70,14 +69,14 @@ private function validateField($request, array $matches, string $type, array $va
7069
$request = $request->addParserBody($name, $default);
7170
continue;
7271
}
73-
$this->doValidation($post[$name], $info);
72+
$this->doValidation($name, $post[$name], $info);
7473
continue;
7574
}
7675
if ($type === ValidatorFrom::PATH) {
7776
if (! isset($matches[$name])) {
7877
continue;
7978
}
80-
$this->doValidation($matches[$name], $info);
79+
$this->doValidation($name, $matches[$name], $info);
8180
continue;
8281
}
8382
}

0 commit comments

Comments
 (0)