验证规则篇
2026年1月12日大约 3 分钟
验证规则篇
如何使用laravel-fast-api-youhujun组件包的自定义验证规则
介绍:
背景:
- 虽然laravel自带了很多验证规则,并且验证不通过的时候返回422.然而在一些开发工具中例如 ApiPost 中 或者 uniapp中无法接收到验证不通过的信息
- 基于上述情况,解决策略是通过自定义验证规则,在验证不通过的时候,通过封装的验证规则异常,截断正常的response响应,直接抛出.
分类:
自定义验证规则分为三种
- 公共位于
App\Rules\Pub这里是一些验证是否是数字,是否是字符串等基础验证 - 通用位于
App\Rules\Common这里是验证与业务无关的 例如 手机号,身份证等 - 与业务逻辑紧密关联的 自定义 建议参考
App\Rules\LaravelFastApi
公共验证规则说明
注意
除了修改好的自定义模版,在 app\Rules\ReplaceRule.php 下也准备了替换模版,同时在生成控制器的时候模版中也使用了公共的验证规则
当你生成控制器的时候,已经use了如下自定义验证规则
use App\Rules\Pub\Required; //必填
use App\Rules\Pub\Numeric; //数字
use App\Rules\Pub\CheckString; //字符串
use App\Rules\Pub\CheckBetween; //字符串长度限定
use App\Rules\Pub\CheckArray; //数组
use App\Rules\Pub\FormatTime; //时间格式
use App\Rules\Pub\CheckUnique; //唯一性校验
use App\Rules\Pub\ChineseCodeNumberLine; //限制为汉字,字母和下划线通用验证规则说明
use App\Rules\Common\IdNumber; //身份证号
use App\Rules\Common\Phone; //手机号特殊验证规则说明
CheckUnique
use App\Rules\Pub\CheckUnique;作用校验字段唯一性.
与传统DB验证不同,这里的验证是通过es进行验证,而不是通过DB查询.
使用方法
示例:
添加
//示例参考
use App\Rules\Pub\Required;
use App\Rules\Pub\Numeric;
use App\Rules\Pub\CheckUnique;
use App\Rules\Pub\CheckString;
use App\Rules\Pub\CheckBetween;
#[DocParams('校验规则')]
public function rules(): array
{
$esIndexName = config('...');
return [
'is_default_option' => ['bail', new Required(), new Numeric()],
'name' => ['bail', new Required(), new CheckString(), new CheckBetween(1, 30), new CheckUnique($esIndexName, 'name')],
'code' => ['bail', 'nullable', new CheckString(), new CheckBetween(1, 30), new CheckUnique($esIndexName, 'code')],
'note' => ['bail', 'nullable', new CheckString(), new CheckBetween(1, 50)],
'sort' => ['bail', new Required(), new Numeric()],
];
}更新
//示例参考
use App\Rules\Pub\Required;
use App\Rules\Pub\Numeric;
use App\Rules\Pub\CheckUnique;
use App\Rules\Pub\CheckString;
use App\Rules\Pub\CheckBetween;
#[DocParams('校验规则')]
public function rules(): array
{
$esIndexName = config('...');
return [
'spec_category_uid' => ['bail', new Required(), new Numeric()],
'is_default_option' => ['bail', new Required(), new Numeric()],
'name' => ['bail', new Required(), new CheckString(), new CheckBetween(1, 30),new CheckUnique($esIndexName, 'name','spec_category_uid',$this->spec_category_uid)],
'code' => ['bail', 'nullable', new CheckString(), new CheckBetween(1, 30),new CheckUnique($esIndexName, 'code','spec_category_uid',$this->spec_category_uid)],
'note' => ['bail', 'nullable', new CheckString(), new CheckBetween(1, 50)],
'sort' => ['bail', new Required(), new Numeric()],
];
}
#[DocParams('验证字段')]
public function validate(array $data): self
{
// 更新唯一校验需要提前拿到主键uid,供rules内CheckUnique忽略自身
$this->spec_category_uid = isset($data['spec_category_uid']) ? $data['spec_category_uid'] : '';
$validator = Validator::make($data, $this->rules(), []);
$validated = $validator->validated();
$requiredFields = ['spec_category_uid', 'is_default_option', 'name', 'sort'];
foreach ($requiredFields as $field) {
if (!isset($validated[$field])) {
throw new RuleException('RuleRequiredError', $field);
}
}
$this->fill($validated);
$this->formatFields();
return $this;
}总结
- 使用方法:CheckUnique四个参数分别是es索引名称,字段名称,更新忽略的雪花uid字段名(可选),更新忽略的雪花uid值(可选)
- 更新场景下,需要提前拿到主键uid,供rules内CheckUnique忽略自身
结语
注意
项目开发阶段,往往工作量较大,中小型公司受限于资金实力限制.人手不足,因此开发阶段以上验证规则足够使用,可以先行只做基础数据类型校验过滤.后续项目再进行专门完善
欢迎同业开发者一起完善自定义验证规则
