Laravel:为 json 字段/列定义规则或架构

Ivá*_*hez 1 validation laravel laravel-5

我的数据库中有一个列,它是 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)
];
Run Code Online (Sandbox Code Playgroud)

但这还不够。

重要提示:这不是重复的问题:Laravel:验证 json 对象

Dan*_*iel 5

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';
    }
}
Run Code Online (Sandbox Code Playgroud)

要将其添加到模型验证中,您只需将该'values'属性分配给Rule 类的新实例:

/**
 * The model validation rules.
 *
 * @var array
 */
public static $rules = [
    'values' => new ValuesSchemaRule,
];
Run Code Online (Sandbox Code Playgroud)