From 1d78d668ca3347bd2213bc9f4ad8efde2a20deb7 Mon Sep 17 00:00:00 2001 From: insolita Date: Mon, 3 Aug 2020 12:32:26 +0800 Subject: [PATCH 1/2] Extend generated validation rules --- src/lib/ValidationRulesBuilder.php | 173 +++++++++++++----- src/lib/items/Attribute.php | 14 +- src/lib/items/ValidationRule.php | 23 +-- tests/specs/blog/models/base/Category.php | 3 +- tests/specs/blog/models/base/Comment.php | 4 +- tests/specs/blog/models/base/Fakerable.php | 17 +- tests/specs/blog/models/base/Post.php | 9 +- tests/specs/blog/models/base/User.php | 9 +- tests/specs/blog_v2/models/base/Category.php | 4 +- tests/specs/blog_v2/models/base/Comment.php | 6 +- tests/specs/blog_v2/models/base/Post.php | 10 +- tests/specs/blog_v2/models/base/PostTag.php | 3 +- tests/specs/blog_v2/models/base/Tag.php | 5 +- tests/specs/blog_v2/models/base/User.php | 10 +- tests/specs/menu/models/base/Menu.php | 2 +- tests/specs/petstore/models/base/Pet.php | 3 +- .../petstore_arrayref/models/base/Pet.php | 3 +- .../petstore_namespace/mymodels/base/Pet.php | 3 +- .../petstore_wrapped/models/base/Pet.php | 3 +- .../specs/petstore_xtable/models/base/Pet.php | 3 +- tests/unit/ValidatorRulesBuilderTest.php | 16 +- 21 files changed, 230 insertions(+), 93 deletions(-) diff --git a/src/lib/ValidationRulesBuilder.php b/src/lib/ValidationRulesBuilder.php index 2c6ef038..077ec7eb 100644 --- a/src/lib/ValidationRulesBuilder.php +++ b/src/lib/ValidationRulesBuilder.php @@ -7,8 +7,12 @@ namespace cebe\yii2openapi\lib; +use cebe\yii2openapi\lib\items\Attribute; use cebe\yii2openapi\lib\items\DbModel; use cebe\yii2openapi\lib\items\ValidationRule; +use function in_array; +use function preg_match; +use function strtolower; class ValidationRulesBuilder { @@ -19,11 +23,14 @@ class ValidationRulesBuilder /** * @var array|ValidationRule[] - **/ + **/ private $rules = []; private $typeScope = [ - 'safe' => [], 'required' => [], 'int' => [], 'bool' => [], 'float' => [], 'string' => [], 'ref' => [] + 'required' => [], + 'ref' => [], + 'trim' => [], + 'safe' => [], ]; public function __construct(DbModel $model) @@ -34,13 +41,124 @@ public function __construct(DbModel $model) /** * @return array|\cebe\yii2openapi\lib\items\ValidationRule[] */ - public function build(): array + public function build():array { $this->prepareTypeScope(); - $this->rulesByType(); + + if (!empty($this->typeScope['trim'])) { + $this->rules[] = new ValidationRule($this->typeScope['trim'], 'trim'); + } + + if (!empty($this->typeScope['required'])) { + $this->rules[] = new ValidationRule($this->typeScope['required'], 'required'); + } + if (!empty($this->typeScope['ref'])) { + $this->addExistRules($this->typeScope['ref']); + } + foreach ($this->model->attributes as $attribute) { + $this->resolveAttributeRules($attribute); + } + if (!empty($this->typeScope['safe'])) { + $this->rules[] = new ValidationRule($this->typeScope['safe'], 'safe'); + } return $this->rules; } + private function resolveAttributeRules(Attribute $attribute):void + { + if ($attribute->isReadOnly()) { + return; + } + if ($attribute->isUnique()) { + $this->rules[] = new ValidationRule([$attribute->columnName], 'unique'); + } + if ($attribute->phpType === 'bool') { + $this->rules[] = new ValidationRule([$attribute->columnName], 'boolean'); + return; + } + + if (in_array($attribute->dbType, ['date', 'time', 'datetime'], true)) { + $this->rules[] = new ValidationRule([$attribute->columnName], $attribute->dbType, []); + return; + } + if (in_array($attribute->phpType, ['int', 'double', 'float']) && !$attribute->isReference()) { + $this->addNumericRule($attribute); + return; + } + if ($attribute->phpType === 'string' && !$attribute->isReference()) { + $this->addStringRule($attribute); + } + if (!empty($attribute->enumValues)) { + $this->rules[] = new ValidationRule([$attribute->columnName], 'in', ['range' => $attribute->enumValues]); + return; + } + $this->addRulesByAttributeName($attribute); + } + + private function addRulesByAttributeName(Attribute $attribute):void + { + //@TODO: probably also patterns for file, image + $patterns = [ + '~e?mail~i' => 'email', + '~(url|site|website|href|link)~i' => 'url', + '~(ip|ipaddr)~i' => 'ip', + ]; + foreach ($patterns as $pattern => $validator) { + if (preg_match($pattern, strtolower($attribute->columnName))) { + $this->rules[] = new ValidationRule([$attribute->columnName], $validator); + return; + } + } + } + + /** + * @param array|Attribute[] $relations + */ + private function addExistRules(array $relations):void + { + foreach ($relations as $attribute) { + if ($attribute->phpType === 'int') { + $this->addNumericRule($attribute); + } elseif ($attribute->phpType === 'string') { + $this->addStringRule($attribute); + } + $this->rules[] = new ValidationRule( + [$attribute->columnName], + 'exist', + ['targetRelation' => $attribute->camelName()] + ); + } + } + + private function addStringRule(Attribute $attribute):void + { + $params = []; + if ($attribute->maxLength === $attribute->minLength && $attribute->minLength !== null) { + $params['length'] = $attribute->minLength; + } else { + if ($attribute->minLength !== null) { + $params['min'] = $attribute->minLength; + } + if ($attribute->maxLength !== null) { + $params['max'] = $attribute->maxLength; + } + } + $this->rules[] = new ValidationRule([$attribute->columnName], 'string', $params); + } + + private function addNumericRule(Attribute $attribute):void + { + $params = []; + if ($attribute->limits['min'] !== null) { + $params['min'] = $attribute->limits['min']; + } + if ($attribute->limits['max'] !== null) { + $params['max'] = $attribute->limits['max']; + } + $validator = $attribute->phpType === 'int' ? 'integer' : 'double'; + $this->rules[] = new ValidationRule([$attribute->columnName], $validator, $params); + } + private function prepareTypeScope():void { foreach ($this->model->attributes as $attribute) { @@ -51,57 +169,20 @@ private function prepareTypeScope():void $this->typeScope['required'][$attribute->columnName] = $attribute->columnName; } - if ($attribute->isReference()) { - if (in_array($attribute->phpType, ['int', 'string'])) { - $this->typeScope[$attribute->phpType][$attribute->columnName] = $attribute->columnName; - } - $this->typeScope['ref'][] = ['attr' => $attribute->columnName, 'rel' => $attribute->camelName()]; - continue; + if ($attribute->phpType === 'string') { + $this->typeScope['trim'][$attribute->columnName] = $attribute->columnName; } - if (in_array($attribute->phpType, ['int', 'string', 'bool', 'float'])) { - $this->typeScope[$attribute->phpType][$attribute->columnName] = $attribute->columnName; + if ($attribute->isReference()) { + $this->typeScope['ref'][] = $attribute; continue; } - if ($attribute->phpType === 'double') { - $this->typeScope['float'][$attribute->columnName] = $attribute->columnName; + if (in_array($attribute->phpType, ['int', 'string', 'bool', 'float', 'double'])) { continue; } $this->typeScope['safe'][$attribute->columnName] = $attribute->columnName; } } - - private function rulesByType():void - { - if (!empty($this->typeScope['string'])) { - $this->rules[] = new ValidationRule($this->typeScope['string'], 'trim'); - } - if (!empty($this->typeScope['required'])) { - $this->rules[] = new ValidationRule($this->typeScope['required'], 'required'); - } - - if (!empty($this->typeScope['int'])) { - $this->rules[] = new ValidationRule($this->typeScope['int'], 'integer'); - } - - foreach ($this->typeScope['ref'] as $relation) { - $this->rules[] = new ValidationRule([$relation['attr']], 'exist', ['targetRelation'=>$relation['rel']]); - } - - if (!empty($this->typeScope['string'])) { - $this->rules[] = new ValidationRule($this->typeScope['string'], 'string'); - } - - if (!empty($this->typeScope['float'])) { - $this->rules[] = new ValidationRule($this->typeScope['float'], 'double'); - } - if (!empty($this->typeScope['bool'])) { - $this->rules[] = new ValidationRule($this->typeScope['bool'], 'boolean'); - } - if (!empty($this->typeScope['safe'])) { - $this->rules[] = new ValidationRule($this->typeScope['safe'], 'safe'); - } - } } diff --git a/src/lib/items/Attribute.php b/src/lib/items/Attribute.php index 6d50b14e..cf44ee65 100644 --- a/src/lib/items/Attribute.php +++ b/src/lib/items/Attribute.php @@ -17,6 +17,8 @@ * @property-write mixed $default * @property-write bool $isPrimary * @property-read string $formattedDescription + * @property-read null|int $maxLength + * @property-read null|int $minLength */ class Attribute extends BaseObject { @@ -27,7 +29,7 @@ class Attribute extends BaseObject public $propertyName; /** - * should be string/integer/boolean/float/double + * should be string/integer/boolean/float/double/array * @var string */ public $phpType = 'string'; @@ -213,6 +215,16 @@ public function camelName():string return Inflector::camelize($this->propertyName); } + public function getMaxLength():?int + { + return $this->size; + } + + public function getMinLength():?int + { + return $this->limits['minLength']; + } + public function getFormattedDescription():string { $comment = $this->columnName.' '.$this->description; diff --git a/src/lib/items/ValidationRule.php b/src/lib/items/ValidationRule.php index f9e2d9af..e95019c7 100644 --- a/src/lib/items/ValidationRule.php +++ b/src/lib/items/ValidationRule.php @@ -7,7 +7,6 @@ namespace cebe\yii2openapi\lib\items; -use yii\base\BaseObject; use yii\helpers\ArrayHelper; use yii\helpers\VarDumper; use function gettype; @@ -15,7 +14,7 @@ use function is_string; use function sprintf; -class ValidationRule extends BaseObject +final class ValidationRule { /**@var array * */ public $attributes = []; @@ -26,29 +25,11 @@ class ValidationRule extends BaseObject /**@var array * */ public $params = []; - public function __construct(array $attributes, string $validator, array $params = [], $config = []) + public function __construct(array $attributes, string $validator, array $params = []) { $this->attributes = array_values($attributes); $this->validator = $validator; $this->params = $params; - parent::__construct($config); - } - - /** - * @param string $key - * @param int|string|array $value - * @return $this - */ - public function addParam(string $key, $value):ValidationRule - { - $this->params[$key] = $value; - return $this; - } - - public function withParams(array $params):ValidationRule - { - $this->params = $params; - return $this; } public function __toString():string diff --git a/tests/specs/blog/models/base/Category.php b/tests/specs/blog/models/base/Category.php index aa670e13..82fb76f4 100644 --- a/tests/specs/blog/models/base/Category.php +++ b/tests/specs/blog/models/base/Category.php @@ -23,7 +23,8 @@ public function rules() return [ [['title'], 'trim'], [['title', 'active'], 'required'], - [['title'], 'string'], + [['title'], 'unique'], + [['title'], 'string', 'max' => 255], [['active'], 'boolean'], ]; } diff --git a/tests/specs/blog/models/base/Comment.php b/tests/specs/blog/models/base/Comment.php index 77d79f0a..fe8b20ed 100644 --- a/tests/specs/blog/models/base/Comment.php +++ b/tests/specs/blog/models/base/Comment.php @@ -25,9 +25,11 @@ public function rules() { return [ [['post_id', 'author_id', 'message', 'created_at'], 'required'], - [['post_id', 'author_id', 'created_at'], 'integer'], + [['post_id'], 'integer'], [['post_id'], 'exist', 'targetRelation' => 'Post'], + [['author_id'], 'integer'], [['author_id'], 'exist', 'targetRelation' => 'Author'], + [['created_at'], 'integer'], [['message'], 'safe'], ]; } diff --git a/tests/specs/blog/models/base/Fakerable.php b/tests/specs/blog/models/base/Fakerable.php index 36171d90..30f29963 100644 --- a/tests/specs/blog/models/base/Fakerable.php +++ b/tests/specs/blog/models/base/Fakerable.php @@ -34,10 +34,21 @@ public function rules() { return [ [['uuid', 'str_text', 'str_varchar', 'str_date', 'str_datetime', 'str_country'], 'trim'], - [['int_min', 'int_max', 'int_minmax', 'int_created_at', 'int_simple'], 'integer'], - [['uuid', 'str_text', 'str_varchar', 'str_date', 'str_datetime', 'str_country'], 'string'], - [['floatval', 'floatval_lim', 'doubleval'], 'double'], [['active'], 'boolean'], + [['floatval'], 'double'], + [['floatval_lim'], 'double', 'min' => 0, 'max' => 1], + [['doubleval'], 'double'], + [['int_min'], 'integer', 'min' => 5], + [['int_max'], 'integer', 'max' => 5], + [['int_minmax'], 'integer', 'min' => 5, 'max' => 25], + [['int_created_at'], 'integer'], + [['int_simple'], 'integer'], + [['uuid'], 'string'], + [['str_text'], 'string'], + [['str_varchar'], 'string', 'max' => 100], + [['str_date'], 'date'], + [['str_datetime'], 'datetime'], + [['str_country'], 'string'], ]; } diff --git a/tests/specs/blog/models/base/Post.php b/tests/specs/blog/models/base/Post.php index ab2fbb16..a8678377 100644 --- a/tests/specs/blog/models/base/Post.php +++ b/tests/specs/blog/models/base/Post.php @@ -29,11 +29,16 @@ public function rules() return [ [['title', 'slug', 'created_at'], 'trim'], [['title', 'category_id', 'active'], 'required'], - [['category_id', 'created_by_id'], 'integer'], + [['category_id'], 'integer'], [['category_id'], 'exist', 'targetRelation' => 'Category'], + [['created_by_id'], 'integer'], [['created_by_id'], 'exist', 'targetRelation' => 'CreatedBy'], - [['title', 'slug', 'created_at'], 'string'], + [['title'], 'unique'], + [['title'], 'string', 'max' => 255], + [['slug'], 'unique'], + [['slug'], 'string', 'min' => 1, 'max' => 200], [['active'], 'boolean'], + [['created_at'], 'date'], ]; } diff --git a/tests/specs/blog/models/base/User.php b/tests/specs/blog/models/base/User.php index ae3da1f0..f9b44936 100644 --- a/tests/specs/blog/models/base/User.php +++ b/tests/specs/blog/models/base/User.php @@ -25,7 +25,14 @@ public function rules() return [ [['username', 'email', 'password', 'role', 'created_at'], 'trim'], [['username', 'email', 'password'], 'required'], - [['username', 'email', 'password', 'role', 'created_at'], 'string'], + [['username'], 'unique'], + [['username'], 'string', 'max' => 200], + [['email'], 'unique'], + [['email'], 'string', 'max' => 200], + [['email'], 'email'], + [['password'], 'string'], + [['role'], 'string', 'max' => 20], + [['created_at'], 'datetime'], ]; } diff --git a/tests/specs/blog_v2/models/base/Category.php b/tests/specs/blog_v2/models/base/Category.php index 9612e7ff..4696fae7 100644 --- a/tests/specs/blog_v2/models/base/Category.php +++ b/tests/specs/blog_v2/models/base/Category.php @@ -24,7 +24,9 @@ public function rules() return [ [['title', 'cover'], 'trim'], [['title', 'cover', 'active'], 'required'], - [['title', 'cover'], 'string'], + [['title'], 'unique'], + [['title'], 'string', 'max' => 100], + [['cover'], 'string'], [['active'], 'boolean'], ]; } diff --git a/tests/specs/blog_v2/models/base/Comment.php b/tests/specs/blog_v2/models/base/Comment.php index 355f9df8..05b2fd86 100644 --- a/tests/specs/blog_v2/models/base/Comment.php +++ b/tests/specs/blog_v2/models/base/Comment.php @@ -26,10 +26,12 @@ public function rules() return [ [['message', 'created_at'], 'trim'], [['post_id', 'message', 'created_at'], 'required'], - [['post_id', 'user_id'], 'integer'], + [['post_id'], 'integer'], [['post_id'], 'exist', 'targetRelation' => 'Post'], + [['user_id'], 'integer'], [['user_id'], 'exist', 'targetRelation' => 'User'], - [['message', 'created_at'], 'string'], + [['message'], 'string'], + [['created_at'], 'datetime'], ]; } diff --git a/tests/specs/blog_v2/models/base/Post.php b/tests/specs/blog_v2/models/base/Post.php index 2e9ea950..b0d66599 100644 --- a/tests/specs/blog_v2/models/base/Post.php +++ b/tests/specs/blog_v2/models/base/Post.php @@ -31,11 +31,17 @@ public function rules() return [ [['title', 'slug', 'lang', 'created_at'], 'trim'], [['title', 'category_id', 'active'], 'required'], - [['category_id', 'created_by_id'], 'integer'], + [['category_id'], 'integer'], [['category_id'], 'exist', 'targetRelation' => 'Category'], + [['created_by_id'], 'integer'], [['created_by_id'], 'exist', 'targetRelation' => 'CreatedBy'], - [['title', 'slug', 'lang', 'created_at'], 'string'], + [['title'], 'unique'], + [['title'], 'string', 'max' => 255], + [['slug'], 'string', 'min' => 1, 'max' => 200], + [['lang'], 'string'], + [['lang'], 'in', 'range' => ['ru', 'eng']], [['active'], 'boolean'], + [['created_at'], 'date'], ]; } diff --git a/tests/specs/blog_v2/models/base/PostTag.php b/tests/specs/blog_v2/models/base/PostTag.php index 1b47cc14..c60d1452 100644 --- a/tests/specs/blog_v2/models/base/PostTag.php +++ b/tests/specs/blog_v2/models/base/PostTag.php @@ -23,8 +23,9 @@ public function rules() { return [ [['post_id', 'tag_id'], 'required'], - [['post_id', 'tag_id'], 'integer'], + [['post_id'], 'integer'], [['post_id'], 'exist', 'targetRelation' => 'Post'], + [['tag_id'], 'integer'], [['tag_id'], 'exist', 'targetRelation' => 'Tag'], ]; } diff --git a/tests/specs/blog_v2/models/base/Tag.php b/tests/specs/blog_v2/models/base/Tag.php index 411b725a..31cf0dda 100644 --- a/tests/specs/blog_v2/models/base/Tag.php +++ b/tests/specs/blog_v2/models/base/Tag.php @@ -23,7 +23,10 @@ public function rules() return [ [['name', 'lang'], 'trim'], [['name', 'lang'], 'required'], - [['name', 'lang'], 'string'], + [['name'], 'unique'], + [['name'], 'string', 'max' => 100], + [['lang'], 'string'], + [['lang'], 'in', 'range' => ['ru', 'eng']], ]; } diff --git a/tests/specs/blog_v2/models/base/User.php b/tests/specs/blog_v2/models/base/User.php index e35e7328..8233b29a 100644 --- a/tests/specs/blog_v2/models/base/User.php +++ b/tests/specs/blog_v2/models/base/User.php @@ -25,7 +25,15 @@ public function rules() return [ [['login', 'email', 'password', 'role', 'created_at'], 'trim'], [['login', 'email', 'password'], 'required'], - [['login', 'email', 'password', 'role', 'created_at'], 'string'], + [['login'], 'unique'], + [['login'], 'string'], + [['email'], 'unique'], + [['email'], 'string'], + [['email'], 'email'], + [['password'], 'string'], + [['role'], 'string'], + [['role'], 'in', 'range' => ['admin', 'editor', 'reader']], + [['created_at'], 'datetime'], ]; } diff --git a/tests/specs/menu/models/base/Menu.php b/tests/specs/menu/models/base/Menu.php index 4f103bc1..5a101927 100644 --- a/tests/specs/menu/models/base/Menu.php +++ b/tests/specs/menu/models/base/Menu.php @@ -28,7 +28,7 @@ public function rules() [['name'], 'required'], [['parent_id'], 'integer'], [['parent_id'], 'exist', 'targetRelation' => 'Parent'], - [['name'], 'string'], + [['name'], 'string', 'min' => 3, 'max' => 100], [['args', 'kwargs'], 'safe'], ]; } diff --git a/tests/specs/petstore/models/base/Pet.php b/tests/specs/petstore/models/base/Pet.php index 243c6bf5..b31123a8 100644 --- a/tests/specs/petstore/models/base/Pet.php +++ b/tests/specs/petstore/models/base/Pet.php @@ -26,7 +26,8 @@ public function rules() [['name'], 'required'], [['store_id'], 'integer'], [['store_id'], 'exist', 'targetRelation' => 'Store'], - [['name', 'tag'], 'string'], + [['name'], 'string'], + [['tag'], 'string'], ]; } diff --git a/tests/specs/petstore_arrayref/models/base/Pet.php b/tests/specs/petstore_arrayref/models/base/Pet.php index 140f111c..ea94410b 100644 --- a/tests/specs/petstore_arrayref/models/base/Pet.php +++ b/tests/specs/petstore_arrayref/models/base/Pet.php @@ -22,7 +22,8 @@ public function rules() return [ [['name', 'tag'], 'trim'], [['name'], 'required'], - [['name', 'tag'], 'string'], + [['name'], 'string'], + [['tag'], 'string'], ]; } diff --git a/tests/specs/petstore_namespace/mymodels/base/Pet.php b/tests/specs/petstore_namespace/mymodels/base/Pet.php index c3331c98..91a1ce64 100644 --- a/tests/specs/petstore_namespace/mymodels/base/Pet.php +++ b/tests/specs/petstore_namespace/mymodels/base/Pet.php @@ -26,7 +26,8 @@ public function rules() [['name'], 'required'], [['store_id'], 'integer'], [['store_id'], 'exist', 'targetRelation' => 'Store'], - [['name', 'tag'], 'string'], + [['name'], 'string'], + [['tag'], 'string'], ]; } diff --git a/tests/specs/petstore_wrapped/models/base/Pet.php b/tests/specs/petstore_wrapped/models/base/Pet.php index 140f111c..ea94410b 100644 --- a/tests/specs/petstore_wrapped/models/base/Pet.php +++ b/tests/specs/petstore_wrapped/models/base/Pet.php @@ -22,7 +22,8 @@ public function rules() return [ [['name', 'tag'], 'trim'], [['name'], 'required'], - [['name', 'tag'], 'string'], + [['name'], 'string'], + [['tag'], 'string'], ]; } diff --git a/tests/specs/petstore_xtable/models/base/Pet.php b/tests/specs/petstore_xtable/models/base/Pet.php index 140f111c..ea94410b 100644 --- a/tests/specs/petstore_xtable/models/base/Pet.php +++ b/tests/specs/petstore_xtable/models/base/Pet.php @@ -22,7 +22,8 @@ public function rules() return [ [['name', 'tag'], 'trim'], [['name'], 'required'], - [['name', 'tag'], 'string'], + [['name'], 'string'], + [['tag'], 'string'], ]; } diff --git a/tests/unit/ValidatorRulesBuilderTest.php b/tests/unit/ValidatorRulesBuilderTest.php index ac73b03e..71793796 100644 --- a/tests/unit/ValidatorRulesBuilderTest.php +++ b/tests/unit/ValidatorRulesBuilderTest.php @@ -27,16 +27,26 @@ public function testBuild() (new Attribute('article'))->setPhpType('string')->setDbType('text')->setDefault(''), (new Attribute('active'))->setPhpType('bool')->setDbType('boolean'), (new Attribute('category'))->asReference('Category') - ->setRequired(true)->setPhpType('int')->setDbType('integer') + ->setRequired(true)->setPhpType('int')->setDbType('integer'), + (new Attribute('state'))->setPhpType('string')->setDbType('string')->setEnumValues(['active', 'draft']), + (new Attribute('created_at'))->setPhpType('string')->setDbType('datetime'), + (new Attribute('contact_email'))->setPhpType('string')->setDbType('string') ], ]); $expected = [ - new ValidationRule(['title', 'article'], 'trim'), + new ValidationRule(['title', 'article', 'state', 'created_at', 'contact_email'], 'trim'), new ValidationRule(['title', 'category_id'], 'required'), new ValidationRule(['category_id'], 'integer'), new ValidationRule(['category_id'], 'exist', ['targetRelation'=>'Category']), - new ValidationRule(['title', 'article'], 'string'), + new ValidationRule(['title'], 'unique'), + new ValidationRule(['title'], 'string', ['max'=>60]), + new ValidationRule(['article'], 'string'), new ValidationRule(['active'], 'boolean'), + new ValidationRule(['state'], 'string'), + new ValidationRule(['state'], 'in', ['range'=>['active', 'draft']]), + new ValidationRule(['created_at'], 'datetime'), + new ValidationRule(['contact_email'], 'string'), + new ValidationRule(['contact_email'], 'email'), ]; $rules = (new ValidationRulesBuilder($model))->build(); From c12d26316b1c30b9c55f16b2e57c3337e7390dee Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Mon, 3 Aug 2020 23:43:23 +0200 Subject: [PATCH 2/2] Apply suggestions from code review --- src/lib/ValidationRulesBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/ValidationRulesBuilder.php b/src/lib/ValidationRulesBuilder.php index 077ec7eb..5445e964 100644 --- a/src/lib/ValidationRulesBuilder.php +++ b/src/lib/ValidationRulesBuilder.php @@ -23,7 +23,7 @@ class ValidationRulesBuilder /** * @var array|ValidationRule[] - **/ + */ private $rules = []; private $typeScope = [