Skip to content

Commit a9e4812

Browse files
committed
修复验证器重写字段描述后bug
1 parent 7c0c395 commit a9e4812

10 files changed

Lines changed: 21 additions & 23 deletions

File tree

LICENSE

100644100755
File mode changed.

README.md

100644100755
File mode changed.

composer.json

100644100755
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,5 @@
3333
},
3434
"config": {
3535
"sort-packages": true
36-
},
37-
"prefer-stable": true
36+
}
3837
}

src/Annotations/RequestValidation.php

100644100755
File mode changed.

src/Annotations/Validation.php

100644100755
File mode changed.

src/Aspect/ValidationAspect.php

100644100755
Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,32 +93,20 @@ private function validationData($validation, $verData, $class, $proceedingJoinPo
9393
}
9494
$rules = $validate->getSceneRule($validation->scene);
9595
}
96+
9697
if ($validate->batch($validation->batch)->check($verData, $rules, $validation->scene) === false) {
9798
throw new ValidateException($validate->getError());
9899
}
99100
if ($validation->security) {
100-
$fields = [];
101-
foreach ($rules as $field => $rule) {
102-
if (is_numeric($field)) {
103-
$field = $rule;
104-
}
105-
$fields[$field] = 1;
106-
}
101+
$fields = $this->getFields($rules);
107102
foreach ($verData as $key => $item) {
108-
if (!isset($fields[$key])) {
103+
if (!in_array($key, $fields)) {
109104
throw new ValidateException('params ' . $key . ' invalid');
110105
}
111106
}
112107
};
113-
114108
if ($validation->filter) {
115-
$fields = [];
116-
foreach ($rules as $field => $rule) {
117-
if (is_numeric($field)) {
118-
$field = $rule;
119-
}
120-
$fields[] = $field;
121-
}
109+
$fields = $this->getFields($rules);
122110
$verData = array_filter($verData, function ($value, $key) use ($fields) {
123111
return in_array($key, $fields);
124112
}, ARRAY_FILTER_USE_BOTH);
@@ -135,4 +123,20 @@ private function validationData($validation, $verData, $class, $proceedingJoinPo
135123
}
136124
}
137125
}
126+
127+
protected function getFields(array $rules)
128+
{
129+
$fields = [];
130+
foreach ($rules as $field => $rule) {
131+
if (is_numeric($field)) {
132+
$field = $rule;
133+
}
134+
if (strpos($field, '|')) {
135+
// 字段|描述 用于指定属性名称
136+
list($field,) = explode('|', $field);
137+
}
138+
$fields[] = $field;
139+
}
140+
return $fields;
141+
}
138142
}

src/ConfigProvider.php

100644100755
File mode changed.

src/Exception/ValidateException.php

100644100755
File mode changed.

src/Validate.php

100644100755
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,6 @@ public function check($data, $rules = [], $scene = '')
418418
$title = isset($this->field[$key]) ? $this->field[$key] : $key;
419419
}
420420

421-
// 场景检测
422-
if (!empty($this->only) && !in_array($key, $this->only)) {
423-
continue;
424-
}
425-
426421
// 获取数据 支持二维数组
427422
$value = $this->getDataValue($data, $key);
428423
switch (true) {

src/ValidateRule.php

100644100755
File mode changed.

0 commit comments

Comments
 (0)