Skip to content

Commit cd34714

Browse files
committed
新增 IgnoreValidatorWhenEmpty
1 parent 48a4c17 commit cd34714

4 files changed

Lines changed: 56 additions & 2 deletions

File tree

src/Attributes/Param.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use EasySwoole\HttpAnnotation\Enum\ParamType;
1010
use EasySwoole\HttpAnnotation\Exception\Annotation;
1111
use EasySwoole\HttpAnnotation\Validator\AbstractInterface\AbstractValidator;
12-
use EasySwoole\HttpAnnotation\Validator\Optional;
1312
use Psr\Http\Message\ServerRequestInterface;
1413

1514
#[\Attribute(\Attribute::TARGET_ALL|\Attribute::IS_REPEATABLE)]

src/Validator/AbstractInterface/AbstractValidator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,14 @@ protected function isIgnoreCheck(Param $param):bool
142142
if($isOptional && (!$param->hasSet()) && ($param->parsedValue() === null)){
143143
return true;
144144
}
145-
//如果自己已经传值。则返回fasle
146145

146+
if(isset($rules['IgnoreValidatorWhenEmpty'])){
147+
if((!$param->hasSet()) || empty($param->parsedValue())){
148+
return true;
149+
}
150+
}
151+
152+
//如果自己已经传值。则返回fasle
147153
if($param->hasSet()){
148154
return false;
149155
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace EasySwoole\HttpAnnotation\Validator;
4+
5+
use EasySwoole\HttpAnnotation\Attributes\Param;
6+
use EasySwoole\HttpAnnotation\Validator\AbstractInterface\AbstractValidator;
7+
use Psr\Http\Message\ServerRequestInterface;
8+
9+
class IgnoreValidatorWhenEmpty extends AbstractValidator
10+
{
11+
12+
function __construct(?string $errorMsg = null)
13+
{
14+
if(empty($errorMsg)){
15+
$errorMsg = "{#name} is ignore validator when value empty";
16+
}
17+
$this->errorMsg($errorMsg);
18+
}
19+
20+
protected function validate(Param $param,ServerRequestInterface $request): bool
21+
{
22+
return true;
23+
}
24+
25+
function ruleName(): string
26+
{
27+
return 'IgnoreValidatorWhenEmpty';
28+
}
29+
}

tests/ControllerExample/Index.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use EasySwoole\HttpAnnotation\Enum\HttpMethod;
1111
use EasySwoole\HttpAnnotation\Enum\ParamFrom;
1212
use EasySwoole\HttpAnnotation\Validator\BigThanColumn;
13+
use EasySwoole\HttpAnnotation\Validator\IgnoreValidatorWhenEmpty;
1314
use EasySwoole\HttpAnnotation\Validator\Integer;
1415
use EasySwoole\HttpAnnotation\Validator\IsUrl;
1516
use EasySwoole\HttpAnnotation\Validator\MaxLength;
@@ -201,4 +202,23 @@ function compare(array $data)
201202
{
202203
$this->writeJson(Status::CODE_OK,$data);
203204
}
205+
206+
207+
#[Api(
208+
apiName: 'ignoreValidatorWhenEmpty',
209+
requestParam: [
210+
new Param(
211+
name: "a",
212+
validate: [
213+
new Integer(),
214+
new IgnoreValidatorWhenEmpty()
215+
],
216+
ignorePassArgWhenNull: true
217+
),
218+
]
219+
)]
220+
function ignoreValidatorWhenEmpty(array $data)
221+
{
222+
$this->writeJson(Status::CODE_OK,$data);
223+
}
204224
}

0 commit comments

Comments
 (0)