Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

验证器type属性改为数组类型,默认值GET/BODY都支持,验证请求时判断请求方式 #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Annotation/Mapping/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class Validate
private $params = [];

/**
* @var string
* @var array
*/
private $type = ValidateType::BODY;
private $type = [ValidateType::GET, ValidateType::BODY];

/**
* @var string
Expand Down Expand Up @@ -76,7 +76,7 @@ public function __construct(array $values)
$this->params = $values['params'];
}
if (isset($values['type'])) {
$this->type = $values['type'];
$this->type = (array) $values['type'];
}
if (isset($values['message'])) {
$this->message = $values['message'];
Expand Down Expand Up @@ -124,9 +124,9 @@ public function getParams(): array
}

/**
* @return string
* @return array
*/
public function getType(): string
public function getType()
{
return $this->type;
}
Expand Down
11 changes: 9 additions & 2 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ public function validate(
* @param array $body
* @param array $validates
* @param array $query
* @param string $requestMethod
*
* @return array
* @throws ValidatorException
*/
public function validateRequest(array $body, array $validates, array $query = []): array
public function validateRequest(array $body, array $validates, array $query = [], string $requestMethod=''): array
{
foreach ($validates as $validateName => $validate) {
$validator = ValidatorRegister::getValidator($validateName);
Expand All @@ -106,9 +107,15 @@ public function validateRequest(array $body, array $validates, array $query = []
$params = $validate['params'] ?? [];

$validateType = $validate['type'];
$isGetValidateType = false;

//�жϸ�validator�Ƿ�֧��GET��ʽ
if (in_array(ValidateType::GET, $validateType)) {
$isGetValidateType = true;
}

// Get query params
if ($validateType == ValidateType::GET) {
if ($requestMethod == RequestMethod::GET && $isGetValidateType) {
$query = $this->validateValidator($query, $type, $validateName, $params, $validator, $fields,
$unfields);
continue;
Expand Down