-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Validator): 增加 Children 规则,用于校验多级数组
- Loading branch information
Showing
4 changed files
with
187 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
/** | ||
* Wei Framework | ||
* | ||
* @copyright Copyright (c) 2008-2020 Twin Huang | ||
* @license http://opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
|
||
namespace Wei\Validator; | ||
|
||
use Wei\V; | ||
use Wei\Validate; | ||
|
||
/** | ||
* Check if the input is validated by the specified V service | ||
* | ||
* @author Twin Huang <twinhuang@qq.com> | ||
* @mixin \ValidateMixin | ||
*/ | ||
class Children extends BaseValidator | ||
{ | ||
protected $typeMessage = '%name% must be array or object'; | ||
|
||
/** | ||
* @var V | ||
*/ | ||
protected $v; | ||
|
||
/** | ||
* @var Validate | ||
*/ | ||
private $selfValidator; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __invoke($input, V $v = null) | ||
{ | ||
$v && $this->storeOption('v', $v); | ||
|
||
return $this->isValid($input); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doValidate($input) | ||
{ | ||
if (!is_array($input) && !is_object($input)) { | ||
$this->addError('type'); | ||
return false; | ||
} | ||
|
||
$options = $this->v->getOptions(); | ||
$this->selfValidator = $this->validate(['data' => $input] + $options); | ||
return $this->selfValidator->isValid(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getMessages($name = null) | ||
{ | ||
if ($this->getErrors()) { | ||
return parent::getMessages($name); | ||
} | ||
return $this->selfValidator->getFlatMessages(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace WeiTest\Validator; | ||
|
||
use Wei\V; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ChildrenTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
wei()->t->setLocale('en'); | ||
} | ||
|
||
public function testChildrenSuc() | ||
{ | ||
$ret = V | ||
::key('configs')->children( | ||
V | ||
::key('key1', '配置1')->minLength(3) | ||
->key('key2', '配置2')->minLength(2) | ||
) | ||
->check([ | ||
'configs' => [ | ||
'key1' => '123', | ||
'key2' => '22', | ||
], | ||
]); | ||
|
||
$this->assertRetSuc($ret); | ||
} | ||
|
||
public function testChildrenErr() | ||
{ | ||
$ret = V | ||
::key('configs')->children( | ||
V | ||
::key('key1', '配置1')->minLength(3) | ||
->key('key2', '配置2')->minLength(2) | ||
) | ||
->check([ | ||
'configs' => [ | ||
'key1' => '1', | ||
'key2' => '2', | ||
], | ||
]); | ||
|
||
$this->assertRetErr($ret, null, '配置1 must have a length greater than 3'); | ||
} | ||
|
||
public function testChildrenNestedErr() | ||
{ | ||
$ret = V | ||
::key('configs') | ||
->children( | ||
V | ||
::key('key1', '配置1')->minLength(3) | ||
->key('key2', '配置2')->children( | ||
V::key('key2.1', '配置2.1')->minLength(2) | ||
) | ||
) | ||
->check([ | ||
'configs' => [ | ||
'key1' => '123', | ||
'key2' => [ | ||
'key2.1' => '1', | ||
], | ||
], | ||
]); | ||
|
||
$this->assertRetErr($ret, null, '配置2.1 must have a length greater than 2'); | ||
} | ||
|
||
public function testInvalidInputType() | ||
{ | ||
$ret = V | ||
::key('configs', '配置')->children( | ||
V::key('key1', '配置1')->minLength(3) | ||
) | ||
->check([ | ||
'configs' => 123, | ||
]); | ||
|
||
$this->assertRetErr($ret, null, '配置 must be array or object'); | ||
} | ||
|
||
protected function getInputTestOptions() | ||
{ | ||
return [ | ||
'v' => V::key('name'), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters