如果为假则返回真 / lumen-strict-types-validation
一个用于Lumen,要求表单请求数据严格类型的验证器
1.0.0
2023-08-24 05:28 UTC
Requires
- php: >=7.4
- laravel/lumen-framework: >=8
Requires (Dev)
- mockery/mockery: ^1.5
- orchestra/testbench: >=5
- phpunit/phpunit: ^9.5
This package is not auto-updated.
Last update: 2024-09-20 08:56:14 UTC
README
确保传入的表单请求数据是特定数据类型。
关于
虽然Lumen自带了许多有用的验证规则,但它没有验证数据类型和内容类型的能力。多年来,对此问题有很多投诉,但由于Laravel的灵活性质,似乎不太可能开始验证像integer
或boolean
这样的规则是否确实验证了数据类型。
此包提供了一种方式,使您可以要求传入的数据必须是给定类型,例如int
、bool
、float
等。
安装
先决条件
- Lumen v8+
- PHP 7.4+
使用Composer安装
composer require trueifnotfalse/lumen-strict-types-validation
将其添加到bootstrap/app.php
并注册服务提供者。
$app->register(TrueIfNotFalse\LumenStrictValidation\Providers\StrictValidationProvider::class);
用法
当构建验证规则时,只需将type-<desired type>
添加到验证规则字符串/数组中。
$rules = [
'id' => 'required|type-int', # This will require the incoming `id` to be an integer.
];
失败信息
失败信息格式为
The :attribute must be of type :type
其中attribute
是要验证的属性(如上述示例中的id
)和type
是期望验证的类型(如上述示例中的int
)。
如果上述示例失败,我们会收到以下信息
The id must be of type int