nwilging/laravel-strict-types-validation

用于在表单请求数据中强制要求严格类型的验证器

1.0.0 2022-05-24 21:10 UTC

This package is auto-updated.

Last update: 2024-09-25 03:10:55 UTC


README

Tests Coverage Latest Stable Version License Total Downloads

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

关于

虽然Laravel自带了许多有用的验证规则,但是它缺少验证数据类型以及内容类型的能力。多年来,对此已经有一些抱怨,但由于Laravel的灵活性,验证规则如integerboolean似乎不太可能开始验证数据是否确实为所需的类型。

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

安装

先决条件

  1. Laravel v8+
  2. PHP 7.4+

使用Composer安装

composer require nwilging/laravel-strict-types-validation

使用方法

当构建验证规则时,只需将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