Skip to content

Commit 34459ad

Browse files
committed
Contains implementation
1 parent ba770ca commit 34459ad

File tree

5 files changed

+99
-10
lines changed

5 files changed

+99
-10
lines changed

TODO.taskpaper

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
TODO.taskpaper
22

33
### Add validation rules by attribute name or pattern #30
4-
create failing test
5-
implement the solution
6-
fix failing tests if any
7-
resolve TODOs if any
4+
create failing test @done (24-08-26 18:13)
5+
implement the solution @done (24-08-26 18:13)
6+
fix failing tests if any @done (24-08-26 18:13)
7+
resolve TODOs if any @done (24-08-26 18:13)
88
☐ review PR
99
☐ delete this file and submit PR

src/lib/ValidationRulesBuilder.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
use cebe\yii2openapi\lib\items\Attribute;
1111
use cebe\yii2openapi\lib\items\DbModel;
1212
use cebe\yii2openapi\lib\items\ValidationRule;
13-
use yii\helpers\VarDumper;
14-
use yii\validators\DateValidator;
1513
use function count;
1614
use function implode;
1715
use function in_array;
@@ -65,7 +63,7 @@ public function build():array
6563
}
6664
}
6765
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()`
66+
// 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()`
6967
if (in_array($attribute->columnName, ['id', $this->model->pkName]) ||
7068
in_array($attribute->propertyName, ['id', $this->model->pkName])
7169
) {
@@ -140,12 +138,30 @@ private function addRulesByAttributeName(Attribute $attribute):void
140138
$patterns = [
141139
'~e?mail~i' => 'email',
142140
'~(url|site|website|href|link)~i' => 'url',
141+
142+
# below patters will only work if `format: binary` (file) is present in OpenAPI spec
143+
# also `string` validation rule will be removed
144+
'~(image|photo|picture)~i' => 'image',
145+
'~(file|pdf|audio|video|document|json|yml|yaml|zip|tar|7z)~i' => 'file',
143146
];
147+
$addRule = function (Attribute $attribute, string $validator): void {
148+
$key = $attribute->columnName . '_' . $validator;
149+
$this->rules[$key] = new ValidationRule([$attribute->columnName], $validator);
150+
};
144151
foreach ($patterns as $pattern => $validator) {
145152
if (empty($attribute->reference) # ignore column name based rules in case of reference/relation # https://github.com/cebe/yii2-openapi/issues/159
146153
&& preg_match($pattern, strtolower($attribute->columnName))) {
147-
$key = $attribute->columnName . '_' . $validator;
148-
$this->rules[$key] = new ValidationRule([$attribute->columnName], $validator);
154+
155+
if (in_array($validator, ['image', 'file'], true)) {
156+
if ($attribute->dbType === 'binary') {
157+
$addRule($attribute, $validator);
158+
// for files, we don't need `string` validation
159+
$key = $attribute->columnName . '_string';
160+
unset($this->rules[$key]);
161+
}
162+
} else {
163+
$addRule($attribute, $validator);
164+
}
149165
return;
150166
}
151167
}
@@ -223,7 +239,7 @@ private function prepareTypeScope():void
223239
if ($attribute->isReadOnly()) {
224240
continue;
225241
}
226-
// 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()`
242+
// 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()`
227243
if (in_array($attribute->columnName, ['id', $this->model->pkName]) ||
228244
in_array($attribute->propertyName, ['id', $this->model->pkName])
229245
) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
return [
4+
'openApiPath' => '@specs/issue_fix/30_add_validation_rules_by_attribute_name_or_pattern/index.yaml',
5+
'generateUrls' => false,
6+
'generateModels' => true,
7+
'excludeModels' => [
8+
'Error',
9+
],
10+
'generateControllers' => false,
11+
'generateMigrations' => false,
12+
'generateModelFaker' => true, // `generateModels` must be `true` in order to use `generateModelFaker` as `true`
13+
];
14+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
openapi: 3.0.3
3+
4+
info:
5+
title: Add validation rules by attribute name or pattern \#30
6+
version: 1.0.0
7+
8+
components:
9+
schemas:
10+
User:
11+
type: object
12+
required:
13+
- id
14+
- name
15+
properties:
16+
id:
17+
type: integer
18+
readOnly: true
19+
name:
20+
description: name
21+
type: string
22+
maxLength: 128
23+
photo:
24+
type: string
25+
format: binary
26+
profile_photo:
27+
type: string
28+
format: binary
29+
pdf:
30+
type: string
31+
format: binary
32+
a_file:
33+
type: string
34+
format: binary
35+
profile:
36+
type: string
37+
38+
paths:
39+
'/':
40+
get:
41+
operationId: opId
42+
summary: summary
43+
responses:
44+
'200':
45+
description: OK

tests/unit/IssueFixTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,18 @@ public function test158BugGiiapiGeneratedRulesEnumWithTrim()
360360
]);
361361
$this->checkFiles($actualFiles, $expectedFiles);
362362
}
363+
364+
// https://github.com/php-openapi/yii2-openapi/issues/30
365+
public function test30AddValidationRulesByAttributeNameOrPattern()
366+
{
367+
$testFile = Yii::getAlias("@specs/issue_fix/30_add_validation_rules_by_attribute_name_or_pattern/index.php");
368+
$this->runGenerator($testFile);
369+
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
370+
// 'recursive' => true,
371+
// ]);
372+
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/30_add_validation_rules_by_attribute_name_or_pattern/mysql"), [
373+
// 'recursive' => true,
374+
// ]);
375+
// $this->checkFiles($actualFiles, $expectedFiles);
376+
}
363377
}

0 commit comments

Comments
 (0)