Skip to content

Commit 910e8f5

Browse files
authoredFeb 20, 2025··
Merge pull request #24 from php-openapi/22-bug-rules-required-is-generated-before-_default
Bug: rules() "required" is generated before "*_default" #22
2 parents 3310737 + b87be74 commit 910e8f5

File tree

66 files changed

+581
-249
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+581
-249
lines changed
 

‎src/lib/ValidationRulesBuilder.php

+29-15
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,38 @@ public function build():array
5353
$this->rules['trim'] = new ValidationRule($this->typeScope['trim'], 'trim');
5454
}
5555

56+
foreach ($this->model->attributes as $attribute) {
57+
if ($this->isIdColumn($attribute)) {
58+
continue;
59+
}
60+
$this->defaultRule($attribute);
61+
}
62+
5663
if (!empty($this->typeScope['required'])) {
5764
$this->rules['required'] = new ValidationRule($this->typeScope['required'], 'required');
5865
}
59-
if (!empty($this->typeScope['ref'])) {
60-
$this->addExistRules($this->typeScope['ref']);
66+
67+
foreach ($this->model->attributes as $attribute) {
68+
if ($this->isIdColumn($attribute)) {
69+
continue;
70+
}
71+
$this->resolveAttributeRules($attribute);
6172
}
73+
6274
foreach ($this->model->indexes as $index) {
6375
if ($index->isUnique) {
6476
$this->addUniqueRule($index->columns);
6577
}
6678
}
67-
foreach ($this->model->attributes as $attribute) {
68-
// 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()`
69-
if (in_array($attribute->columnName, ['id', $this->model->pkName]) ||
70-
in_array($attribute->propertyName, ['id', $this->model->pkName])
71-
) {
72-
continue;
73-
}
74-
$this->resolveAttributeRules($attribute);
79+
80+
if (!empty($this->typeScope['ref'])) {
81+
$this->addExistRules($this->typeScope['ref']);
7582
}
7683

7784
if (!empty($this->typeScope['safe'])) {
7885
$this->rules['safe'] = new ValidationRule($this->typeScope['safe'], 'safe');
7986
}
87+
8088
return $this->rules;
8189
}
8290

@@ -93,7 +101,6 @@ private function resolveAttributeRules(Attribute $attribute):void
93101
}
94102
if ($attribute->phpType === 'bool' || $attribute->phpType === 'boolean') {
95103
$this->rules[$attribute->columnName . '_boolean'] = new ValidationRule([$attribute->columnName], 'boolean');
96-
$this->defaultRule($attribute);
97104
return;
98105
}
99106

@@ -111,13 +118,11 @@ private function resolveAttributeRules(Attribute $attribute):void
111118
}
112119

113120
$this->rules[$key] = new ValidationRule([$attribute->columnName], $attribute->dbType, $params);
114-
$this->defaultRule($attribute);
115121
return;
116122
}
117123

118124
if (in_array($attribute->phpType, ['int', 'integer', 'double', 'float']) && !$attribute->isReference()) {
119125
$this->addNumericRule($attribute);
120-
$this->defaultRule($attribute);
121126
return;
122127
}
123128
if ($attribute->phpType === 'string' && !$attribute->isReference()) {
@@ -127,10 +132,8 @@ private function resolveAttributeRules(Attribute $attribute):void
127132
$key = $attribute->columnName . '_in';
128133
$this->rules[$key] =
129134
new ValidationRule([$attribute->columnName], 'in', ['range' => $attribute->enumValues]);
130-
$this->defaultRule($attribute);
131135
return;
132136
}
133-
$this->defaultRule($attribute);
134137
$this->addRulesByAttributeName($attribute);
135138
}
136139

@@ -278,4 +281,15 @@ public function __toString()
278281
}
279282
};
280283
}
284+
285+
private function isIdColumn(Attribute $attribute): bool
286+
{
287+
// 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()`
288+
if (in_array($attribute->columnName, ['id', $this->model->pkName]) ||
289+
in_array($attribute->propertyName, ['id', $this->model->pkName])
290+
) {
291+
return true;
292+
}
293+
return false;
294+
}
281295
}

‎tests/DbTestCase.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
namespace tests;
44

55
use cebe\yii2openapi\generator\ApiGenerator;
6+
use SamIT\Yii2\MariaDb\Schema as MariaDbSchema;
67
use Yii;
78
use yii\db\IndexConstraint;
8-
use yii\di\Container;
99
use yii\db\mysql\Schema as MySqlSchema;
1010
use yii\db\pgsql\Schema as PgSqlSchema;
11-
use \SamIT\Yii2\MariaDb\Schema as MariaDbSchema;
12-
use yii\helpers\{ArrayHelper, VarDumper, StringHelper, Console};
11+
use yii\di\Container;
12+
use yii\helpers\{ArrayHelper, StringHelper};
1313
use yii\helpers\FileHelper;
1414

1515
class DbTestCase extends \PHPUnit\Framework\TestCase
@@ -102,7 +102,6 @@ protected function compareFiles(array $actual, string $testFile)
102102
foreach ($actual as $file) {
103103
$expectedFile = str_replace('@app', substr($testFile, 0, -4), $file);
104104
$actualFile = str_replace('@app', Yii::getAlias('@app'), $file);
105-
// exec('cp '.$actualFile.' '.$expectedFile);
106105
$this->checkFiles([$actualFile], [$expectedFile]);
107106
}
108107
}
@@ -128,6 +127,7 @@ protected function checkFiles(array $actual, array $expected)
128127
);
129128
}
130129

130+
// exec('cp '.$file.' '.$expectedFilePath);
131131
$this->assertFileEquals($expectedFilePath, $file, "Failed asserting that file contents of\n$file\nare equal to file contents of\n$expectedFilePath \n\n cp $file $expectedFilePath \n\n ");
132132
}
133133
}

0 commit comments

Comments
 (0)
Please sign in to comment.