Skip to content

Commit 79574a8

Browse files
committed
改进依赖和验证方法的异常处理
1 parent f20b4a4 commit 79574a8

3 files changed

Lines changed: 14 additions & 24 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"ext-pdo": "*",
1919
"psr/simple-cache": ">=1.0",
2020
"psr/log": ">=1.0",
21-
"topthink/think-helper":"^3.1"
21+
"topthink/think-helper":"^3.1",
22+
"topthink/think-validate":"^3.0"
2223
},
2324
"require-dev": {
2425
"phpunit/phpunit": "^9.6|^10"

src/Model.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use ArrayAccess;
1717
use Closure;
18-
use InvalidArgumentException;
1918
use JsonSerializable;
2019
use think\contract\Arrayable;
2120
use think\contract\Jsonable;
@@ -320,21 +319,16 @@ protected function parseValidate(): string
320319
* @param array $data 数据
321320
* @param array $allow 需要验证的字段
322321
*
323-
* @throws InvalidArgumentException
322+
* @throws ValidateException
324323
* @return void
325324
*/
326325
protected function validate(array $data, array $allow = []): void
327326
{
328327
$validater = $this->getOption('validate');
329-
if (!empty($validater) && class_exists('think\validate')) {
330-
try {
331-
validate($validater)
332-
->only($allow ?: array_keys($data))
333-
->check($data);
334-
} catch (ValidateException $e) {
335-
// 验证失败 输出错误信息
336-
throw new InvalidArgumentException($e->getError());
337-
}
328+
if (!empty($validater)) {
329+
validate($validater)
330+
->only($allow ?: array_keys($data))
331+
->check($data);
338332
}
339333
}
340334

src/model/View.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace think\model;
1515

16-
use InvalidArgumentException;
1716
use ReflectionClass;
1817
use ReflectionProperty;
1918
use think\Entity;
@@ -324,23 +323,19 @@ protected function convertData(): array
324323
* @param array $data 数据
325324
* @param array $allow 需要验证的字段
326325
*
327-
* @throws InvalidArgumentException
326+
* @throws ValidateException
328327
* @return array
329328
*/
330329
protected function validate(array $data = [], array $allow = []): array
331330
{
332331
$validater = $this->getOption('validate');
333-
if (!empty($validater) && class_exists('think\validate')) {
334-
$data = $data ?: $this->getData();
335-
try {
336-
return validate($validater)
337-
->only($allow ?: array_keys($data))
338-
->checked($data);
339-
} catch (ValidateException $e) {
340-
// 验证失败 输出错误信息
341-
throw new InvalidArgumentException($e->getError());
342-
}
332+
$data = $data ?: $this->getData();
333+
if (!empty($validater)) {
334+
return validate($validater)
335+
->only($allow ?: array_keys($data))
336+
->checked($data);
343337
}
338+
return $data;
344339
}
345340

346341
/**

0 commit comments

Comments
 (0)