如果为假则返回真/lumen-strict-types-validation

一个用于Lumen,要求表单请求数据严格类型的验证器

1.0.0 2023-08-24 05:28 UTC

This package is not auto-updated.

Last update: 2024-09-20 08:56:14 UTC


README

确保传入的表单请求数据是特定数据类型。

关于

虽然Lumen自带了许多有用的验证规则,但它没有验证数据类型和内容类型的能力。多年来,对此问题有很多投诉,但由于Laravel的灵活性质,似乎不太可能开始验证像integerboolean这样的规则是否确实验证了数据类型。

此包提供了一种方式,使您可以要求传入的数据必须是给定类型,例如intboolfloat等。

安装

先决条件

  1. Lumen v8+
  2. 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