使用 Laravel 8,在 JSON POST 请求中,我在字符串引号中有一个字段
{ "is_active": "1"}
。在 Laravel 中进行以下验证
'is_active' => ['required','integer',Rule::in([0, 1]) ]
它接受整数 1,甚至用引号引起来。如何严格检查,只允许
{ "is_active": 1}
类型 转换通过在要转换的值之前的括号内写入类型来将值转换为所选类型。允许的转换为:(int) - 转换为 int。
https://laravel.com/docs/8.x/eloquent-mutators#cast-parameters
例如
我们有一个名为 cast 的模型属性,您可以在其中指定列名称,如下所示:
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'rating' => 'integer',
];
要检查数据类型持续时间验证,请使用此包:
https://github.com/qtlenh/laravel-strict-validator
将验证规则更改为:
'is_active' => ['integer:strict',...]