Skip to content

Commit

Permalink
Improve Validator, passing property name now, and use property name i…
Browse files Browse the repository at this point in the history
…n exception message
  • Loading branch information
huangzhhui committed Mar 7, 2018
1 parent 98d3812 commit c27b40a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
21 changes: 6 additions & 15 deletions src/Middleware/ValidatorMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,30 @@
use Swoft\Http\Server\Validator\HttpValidator;

/**
* validator of request
*
* Validator middleware
* @Bean()
* @uses ValidatorMiddleware
* @version 2017年12月03日
* @author stelin <[email protected]>
* @copyright Copyright 2010-2016 swoft software
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
*/
class ValidatorMiddleware implements MiddlewareInterface
{
/**
* do process
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Psr\Http\Server\RequestHandlerInterface $handler
*
* @return \Psr\Http\Message\ResponseInterface
* @throws \Swoft\Exception\ValidatorException
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$httpHandler = $request->getAttribute(AttributeEnum::ROUTER_ATTRIBUTE);
$info = $httpHandler[2];
$info = $httpHandler[2];

if (isset($info['handler']) && \is_string($info['handler'])) {
$exploded = explode('@', $info['handler']);
$className = $exploded[0] ?? '';
$exploded = explode('@', $info['handler']);
$className = $exploded[0] ?? '';
$validatorKey = $exploded[1] ?? '';
$matches = $info['matches']??[];
$matches = $info['matches'] ?? [];

/* @var HttpValidator $validator */
$validator = App::getBean(HttpValidator::class);
$validator = App::getBean(HttpValidator::class);
$collector = ValidatorCollector::getCollector();

if (isset($collector[$className][$validatorKey]['validator'])) {
Expand Down
13 changes: 6 additions & 7 deletions src/Validator/HttpValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@
class HttpValidator extends AbstractValidator
{
/**
* validate
* Validate
*
* @param mixed $validators
* @param array ...$params
* @return mixed
* @throws \Swoft\Exception\ValidatorException
*/
public function validate($validators, ...$params)
public function validate(...$params)
{
/**
* @var Request $request
* @var array $matches
*/
list($request, $matches) = $params;
list($validators, $request, $matches) = $params;

if (! \is_array($validators)) {
return $request;
Expand Down Expand Up @@ -61,7 +60,7 @@ private function validateField($request, array $matches, string $type, array $va
$request = $request->addQueryParam($name, $default);
continue;
}
$this->doValidation($get[$name], $info);
$this->doValidation($name, $get[$name], $info);

continue;
}
Expand All @@ -70,14 +69,14 @@ private function validateField($request, array $matches, string $type, array $va
$request = $request->addParserBody($name, $default);
continue;
}
$this->doValidation($post[$name], $info);
$this->doValidation($name, $post[$name], $info);
continue;
}
if ($type === ValidatorFrom::PATH) {
if (! isset($matches[$name])) {
continue;
}
$this->doValidation($matches[$name], $info);
$this->doValidation($name, $matches[$name], $info);
continue;
}
}
Expand Down

0 comments on commit c27b40a

Please sign in to comment.