Skip to content

Commit

Permalink
feat(Validator): 增加 Children 规则,用于校验多级数组
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Oct 28, 2020
1 parent d6d6989 commit 9c39028
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 5 deletions.
69 changes: 69 additions & 0 deletions lib/Validator/Children.php
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();
}
}
3 changes: 3 additions & 0 deletions lib/Validator/i18n/zh-CN.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
'%name% must be %length% characters' => '%name%必须是%length%个字符',
'%name% must be between %min% to %max% characters' => '%name%必须包含%min%-%max%个字符',

// children
'%name% must be array or object' => '%name%必须是数组或对象',

// chinese
'%name% must contain only Chinese characters' => '%name%只能由中文组成',
'%name% must not contain only Chinese characters' => '%name%不能只由中文组成',
Expand Down
97 changes: 97 additions & 0 deletions tests/unit/Validator/ChildrenTest.php
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'),
];
}
}
23 changes: 18 additions & 5 deletions tests/unit/Validator/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,23 @@ public function providerForInput()
// Initial test fixtures
$data = [
// boolean
true, false,
true,
false,
// integer
1234, -123, 0123, 0x1A,
1234,
-123,
0123,
0x1A,
// float
1.234, 1.2e3, 7E-10,
1.234,
1.2e3,
7E-10,
// string
'this is a simple string',
// object
new \stdClass(), new \ArrayObject([1, 3]), new \DateTime(),
new \stdClass(),
new \ArrayObject([1, 3]),
new \DateTime(),
// resource
curl_init(),
// null
Expand All @@ -71,7 +79,7 @@ public function testInput($input)
{
// Gets validator name WeiTest\Validator\LengthTest => Length
$name = $this->name ?: substr(static::class, strrpos(static::class, '\\') + 1, -4);
$validator = $this->validate->createRuleValidator($name, $this->inputTestOptions);
$validator = $this->validate->createRuleValidator($name, $this->getInputTestOptions());

// The validator should accept any type of INPUT and do NOT raise any
// exceptions or errors
Expand All @@ -86,4 +94,9 @@ public function createResource()

return static::$resource;
}

protected function getInputTestOptions()
{
return $this->inputTestOptions;
}
}

0 comments on commit 9c39028

Please sign in to comment.