|
10 | 10 | use cebe\yii2openapi\lib\items\Attribute; |
11 | 11 | use cebe\yii2openapi\lib\items\DbModel; |
12 | 12 | use cebe\yii2openapi\lib\items\ValidationRule; |
13 | | -use yii\helpers\VarDumper; |
14 | | -use yii\validators\DateValidator; |
15 | 13 | use function count; |
16 | 14 | use function implode; |
17 | 15 | use function in_array; |
@@ -65,7 +63,7 @@ public function build():array |
65 | 63 | } |
66 | 64 | } |
67 | 65 | 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()` |
69 | 67 | if (in_array($attribute->columnName, ['id', $this->model->pkName]) || |
70 | 68 | in_array($attribute->propertyName, ['id', $this->model->pkName]) |
71 | 69 | ) { |
@@ -140,12 +138,30 @@ private function addRulesByAttributeName(Attribute $attribute):void |
140 | 138 | $patterns = [ |
141 | 139 | '~e?mail~i' => 'email', |
142 | 140 | '~(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', |
143 | 146 | ]; |
| 147 | + $addRule = function (Attribute $attribute, string $validator): void { |
| 148 | + $key = $attribute->columnName . '_' . $validator; |
| 149 | + $this->rules[$key] = new ValidationRule([$attribute->columnName], $validator); |
| 150 | + }; |
144 | 151 | foreach ($patterns as $pattern => $validator) { |
145 | 152 | if (empty($attribute->reference) # ignore column name based rules in case of reference/relation # https://github.com/cebe/yii2-openapi/issues/159 |
146 | 153 | && 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 | + } |
149 | 165 | return; |
150 | 166 | } |
151 | 167 | } |
@@ -223,7 +239,7 @@ private function prepareTypeScope():void |
223 | 239 | if ($attribute->isReadOnly()) { |
224 | 240 | continue; |
225 | 241 | } |
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()` |
227 | 243 | if (in_array($attribute->columnName, ['id', $this->model->pkName]) || |
228 | 244 | in_array($attribute->propertyName, ['id', $this->model->pkName]) |
229 | 245 | ) { |
|
0 commit comments