File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99use EasySwoole \HttpAnnotation \Enum \ParamType ;
1010use EasySwoole \HttpAnnotation \Exception \Annotation ;
1111use EasySwoole \HttpAnnotation \Validator \AbstractInterface \AbstractValidator ;
12- use EasySwoole \HttpAnnotation \Validator \Optional ;
1312use Psr \Http \Message \ServerRequestInterface ;
1413
1514#[\Attribute(\Attribute::TARGET_ALL |\Attribute::IS_REPEATABLE )]
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1010use EasySwoole \HttpAnnotation \Enum \HttpMethod ;
1111use EasySwoole \HttpAnnotation \Enum \ParamFrom ;
1212use EasySwoole \HttpAnnotation \Validator \BigThanColumn ;
13+ use EasySwoole \HttpAnnotation \Validator \IgnoreValidatorWhenEmpty ;
1314use EasySwoole \HttpAnnotation \Validator \Integer ;
1415use EasySwoole \HttpAnnotation \Validator \IsUrl ;
1516use 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}
You can’t perform that action at this time.
0 commit comments