Skip to content

Commit 0f9c097

Browse files
committed
Implement feedback
1 parent c0bdf96 commit 0f9c097

File tree

5 files changed

+45
-22
lines changed

5 files changed

+45
-22
lines changed

src/lib/ValidationRulesBuilder.php

Lines changed: 37 additions & 18 deletions
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-
if (!empty($this->typeScope['ref'])) {
57-
$this->addExistRules($this->typeScope['ref']);
58-
}
59-
foreach ($this->model->indexes as $index) {
60-
if ($index->isUnique) {
61-
$this->addUniqueRule($index->columns);
56+
foreach ($this->model->attributes as $attribute) {
57+
if ($this->isIdColumn($attribute)) {
58+
continue;
6259
}
60+
$this->defaultRule($attribute);
61+
}
62+
63+
if (!empty($this->typeScope['required'])) {
64+
$this->rules['required'] = new ValidationRule($this->typeScope['required'], 'required');
6365
}
66+
6467
foreach ($this->model->attributes as $attribute) {
65-
// 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()`
66-
if (in_array($attribute->columnName, ['id', $this->model->pkName]) ||
67-
in_array($attribute->propertyName, ['id', $this->model->pkName])
68-
) {
68+
if ($this->isIdColumn($attribute)) {
6969
continue;
7070
}
7171
$this->resolveAttributeRules($attribute);
7272
}
7373

74+
foreach ($this->model->indexes as $index) {
75+
if ($index->isUnique) {
76+
$this->addUniqueRule($index->columns);
77+
}
78+
}
79+
80+
if (!empty($this->typeScope['ref'])) {
81+
$this->addExistRules($this->typeScope['ref']);
82+
}
83+
7484
if (!empty($this->typeScope['safe'])) {
7585
$this->rules['safe'] = new ValidationRule($this->typeScope['safe'], 'safe');
7686
}
77-
if (!empty($this->typeScope['required'])) {
78-
$this->rules['required'] = new ValidationRule($this->typeScope['required'], 'required');
79-
}
87+
8088
return $this->rules;
8189
}
8290

@@ -93,7 +101,7 @@ 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);
104+
// $this->defaultRule($attribute);
97105
return;
98106
}
99107

@@ -111,13 +119,13 @@ private function resolveAttributeRules(Attribute $attribute):void
111119
}
112120

113121
$this->rules[$key] = new ValidationRule([$attribute->columnName], $attribute->dbType, $params);
114-
$this->defaultRule($attribute);
122+
// $this->defaultRule($attribute);
115123
return;
116124
}
117125

118126
if (in_array($attribute->phpType, ['int', 'integer', 'double', 'float']) && !$attribute->isReference()) {
119127
$this->addNumericRule($attribute);
120-
$this->defaultRule($attribute);
128+
// $this->defaultRule($attribute);
121129
return;
122130
}
123131
if ($attribute->phpType === 'string' && !$attribute->isReference()) {
@@ -127,10 +135,10 @@ private function resolveAttributeRules(Attribute $attribute):void
127135
$key = $attribute->columnName . '_in';
128136
$this->rules[$key] =
129137
new ValidationRule([$attribute->columnName], 'in', ['range' => $attribute->enumValues]);
130-
$this->defaultRule($attribute);
138+
// $this->defaultRule($attribute); // TODO remove
131139
return;
132140
}
133-
$this->defaultRule($attribute);
141+
// $this->defaultRule($attribute);
134142
$this->addRulesByAttributeName($attribute);
135143
}
136144

@@ -278,4 +286,15 @@ public function __toString()
278286
}
279287
};
280288
}
289+
290+
private function isIdColumn(Attribute $attribute): bool
291+
{
292+
// 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()`
293+
if (in_array($attribute->columnName, ['id', $this->model->pkName]) ||
294+
in_array($attribute->propertyName, ['id', $this->model->pkName])
295+
) {
296+
return true;
297+
}
298+
return false;
299+
}
281300
}

tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/index.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ components:
2323
maxLength: 128
2424
paymentMethodName:
2525
type: string
26+
default: card
2627
verified:
2728
type: boolean
2829
default: false

tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql/migrations_mysql_db/m200000_000000_create_table_accounts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function up()
99
{
1010
$this->createTable('{{%accounts}}', [
1111
'id' => $this->primaryKey(),
12-
'name' => $this->string(128)->notNull(),
12+
'name' => $this->string(128)->notNull()->comment('account name'),
1313
'paymentMethodName' => $this->text()->null(),
1414
'verified' => $this->boolean()->notNull()->defaultValue(false),
1515
]);

tests/specs/issue_fix/22_bug_rules_required_is_generated_before_default/mysql/models/base/Account.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
22

3+
/**
4+
* This file is generated by Gii, do not change manually!
5+
*/
6+
37
namespace app\models\base;
48

59
/**
@@ -22,11 +26,11 @@ public function rules()
2226
{
2327
return [
2428
'trim' => [['name', 'paymentMethodName'], 'trim'],
29+
'required' => [['name', 'verified'], 'required'],
2530
'name_string' => [['name'], 'string', 'max' => 128],
2631
'paymentMethodName_string' => [['paymentMethodName'], 'string'],
2732
'verified_boolean' => [['verified'], 'boolean'],
2833
'verified_default' => [['verified'], 'default', 'value' => false],
29-
'required' => [['name', 'verified'], 'required'],
3034
];
3135
}
3236
}

tests/unit/IssueFixTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ public function test74InvalidSchemaReferenceError()
10011001
$this->runActualMigrations();
10021002
}
10031003

1004-
// https://github.com/php-openapi/yii2-openapi/issues/22
1004+
// https://github.com/php-openapi/yii2-openapi/issues/22
10051005
public function test22BugRulesRequiredIsGeneratedBeforeDefault()
10061006
{
10071007
$testFile = Yii::getAlias("@specs/issue_fix/22_bug_rules_required_is_generated_before_default/index.php");
@@ -1014,5 +1014,4 @@ public function test22BugRulesRequiredIsGeneratedBeforeDefault()
10141014
]);
10151015
$this->checkFiles($actualFiles, $expectedFiles);
10161016
}
1017-
10181017
}

0 commit comments

Comments
 (0)