Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 8a89624

Browse files
authored
Merge pull request #12 from SOHELAHMED7/129-check-x-pk-when-generating-validation-rules
Resolve - Check x-pk when generating validation rules
2 parents 7219589 + 147a0b9 commit 8a89624

File tree

5 files changed

+92
-2
lines changed

5 files changed

+92
-2
lines changed

src/lib/ValidationRulesBuilder.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public function build():array
6464
}
6565
foreach ($this->model->attributes as $attribute) {
6666
// column/field/property with name `id` is considered as Primary Key by this library and it is automatically handled by DB/Yii; so remove it from validation `rules()`
67-
if ($attribute->columnName === 'id' || $attribute->propertyName === 'id') {
67+
if (in_array($attribute->columnName, ['id', $this->model->pkName]) ||
68+
in_array($attribute->propertyName, ['id', $this->model->pkName])
69+
) {
6870
continue;
6971
}
7072
$this->resolveAttributeRules($attribute);
@@ -186,7 +188,9 @@ private function prepareTypeScope():void
186188
continue;
187189
}
188190
// column/field/property with name `id` is considered as Primary Key by this library and it is automatically handled by DB/Yii; so remove it from validation `rules()`
189-
if ($attribute->columnName === 'id' || $attribute->propertyName === 'id') {
191+
if (in_array($attribute->columnName, ['id', $this->model->pkName]) ||
192+
in_array($attribute->propertyName, ['id', $this->model->pkName])
193+
) {
190194
continue;
191195
}
192196
if ($attribute->defaultValue === null && $attribute->isRequired()) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace app\models;
4+
5+
class Post extends \app\models\base\Post
6+
{
7+
8+
9+
}
10+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace app\models;
3+
4+
use Faker\UniqueGenerator;
5+
6+
/**
7+
* Fake data generator for Post
8+
* @method static Post makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
9+
* @method static Post saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
10+
* @method static Post[] make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
11+
* @method static Post[] save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
12+
*/
13+
class PostFaker extends BaseModelFaker
14+
{
15+
16+
/**
17+
* @param array|callable $attributes
18+
* @return Post|\yii\db\ActiveRecord
19+
* @example
20+
* $model = (new PostFaker())->generateModels(['author_id' => 1]);
21+
* $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) {
22+
* $model->scenario = 'create';
23+
* $model->author_id = 1;
24+
* return $model;
25+
* });
26+
**/
27+
public function generateModel($attributes = [])
28+
{
29+
$faker = $this->faker;
30+
$uniqueFaker = $this->uniqueFaker;
31+
$model = new Post();
32+
$model->uid = substr($uniqueFaker->sha256, 0, 255);
33+
$model->title = $faker->sentence;
34+
if (!is_callable($attributes)) {
35+
$model->setAttributes($attributes, false);
36+
} else {
37+
$model = $attributes($model, $faker, $uniqueFaker);
38+
}
39+
return $model;
40+
}
41+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace app\models\base;
4+
5+
/**
6+
*
7+
*
8+
* @property string $uid
9+
* @property string $title
10+
*
11+
*/
12+
abstract class Post extends \yii\db\ActiveRecord
13+
{
14+
public static function tableName()
15+
{
16+
return '{{%posts}}';
17+
}
18+
19+
public function rules()
20+
{
21+
return [
22+
'trim' => [['title'], 'trim'],
23+
'title_string' => [['title'], 'string'],
24+
];
25+
}
26+
}

tests/specs/id_not_in_rules/id_not_in_rules.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,12 @@ components:
2424
type: integer
2525
name:
2626
type: string
27+
Post:
28+
x-table: posts
29+
x-pk: uid
30+
properties:
31+
uid:
32+
type: string
33+
title:
34+
type: string
35+

0 commit comments

Comments
 (0)