【问题标题】:Laravel: Defining rules or schema for a json field/columnLaravel:为 json 字段/列定义规则或模式
【发布时间】:2018-12-08 23:15:29
【问题描述】:
我的数据库中有一个列,它是 json 类型:values。
但我需要强制执行架构。
- 必须是数组
- 每个对象必须有 2 个且只有 2 个属性(必需)。
code_name 和 description
-
code_name 在 JSON 数组中必须是唯一的
Laravel 是否为此提供了一些开箱即用的功能?还是我需要在创建和更新时手动解码 json 以验证该规则?
到现在为止。我的模型中只有这个验证规则:
/**
* The model validation rules.
*
* @var array
*/
public static $rules = [
'values' => 'required|json', // TO BE CHECKED (values validation (json schema) and setter)
];
但这还不够。
重要提示:这不是重复的问题:Laravel: validate json object
【问题讨论】:
标签:
laravel
validation
laravel-5
【解决方案1】:
Laravel 支持添加你自己的自定义验证规则。
要创建验证规则,您应该创建一个实现接口Illuminate\Contracts\Validation\Rule 的新类。
artisan 命令php artisan make:rule {NAME} 会在App\Rules 命名空间中自动为您生成一个规则模板。
简单地说,您编写了一个passes($attribute, $value) 函数,该函数返回一个布尔值,用于确定验证是失败还是成功。
我根据您的要求在下面编写了一个示例。
示例
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class ValuesSchemaRule implements Rule
{
private $validProperties = ['code_name', 'description'];
public function __construct()
{
}
public function passes($attribute, $value)
{
$array = json_decode($value);
if (is_array($array) === false) {
return false;
}
$codeNames = [];
foreach ($array as $object) {
$properties = get_object_vars($object);
if (count($properties) !== 2) {
return false;
}
$propertyNames = array_keys($properties);
if (in_array($this->validProperties, $propertyNames) === false) {
return false;
}
array_push($codeNames, $object->code_name);
}
if (count($codeNames) !== count(array_unique($codeNames))) {
return false;
}
return true;
}
public function message()
{
return 'The values does not comply to the JSON schema';
}
}
要将其添加到模型验证中,您只需将 'values' 属性分配给 Rule 类的新实例:
/**
* The model validation rules.
*
* @var array
*/
public static $rules = [
'values' => new ValuesSchemaRule,
];