如何按照 Apigility 方式验证嵌套数据?

How to validate nested data following Apigility way?

我有一个关于验证嵌套 json 数据的小问题。例如,我有类似的 PATCH 请求:

{
    "awesome": "yes",
    "myObject": {
        "some_property": "Text Example value",
        "another_property": "1965"
    }
}

为此嵌套数据 some_propertyanother_property 设置过滤器和验证器的正确方法是什么?

非常感谢您的回答

您可以像通常在 ZF2 中为字段集所做的那样,在过滤器和验证器配置中设置嵌套数据。

return array(
    'awesome' => array(
        'name' => 'awesome',
        'required' => true,
        'filters' => array(
            //...
        ),
        'validators' => array(
            //...
        )
    ),
    'myObject' => array(
        'some_property' => array(
            'name' => 'some_property',
            'required' => true,
            'filters' => array(
                //...    
            ),
            'validators' => array(
                //...
            )
        ),
        'another_property' => array(
            'name' => 'another_property',
            'required' => true,
            'filters' => array(
                //...    
            ),
            'validators' => array(
                //...
            )
        ),
        // Type key necessary for nested fields
        'type' => 'Zend\InputFilter\InputFilter'
    )
);

查看更多信息以及如何配置 ZF 内容验证another question on StackOverfow here or the Content-Validation module documentation here

我知道这个答案已经很晚了。我偶然发现了同样的问题(与 Apigility 无关)。经过大量尝试和错误后,我发现了一个完全有效的 InputFilter 规范,用于验证嵌套字段/集合以及名为 type 的键。如果其他人发现这个,请在​​此处添加以供参考(你好,未来的我)。

嵌套对象

Wilt 已回答,为完整性添加。

$data = [
    'root-key' => [
        'sub-key' => 'my-value',
        'sub-key2' => 'my-other-value',
    ],
    'simple-key' => 'simple-value'
];

'input_filter_specs' => [
    'my-filter' => [
        'root-key' => [
            'type' => InputFilter::class,
            'sub-key' => [
                'required' => true,
                'filters' => [ /** Add filters **/ ],
                'validators' => [ /** Add validators **/],
            ],
            'sub-key2' => [
                'required' => true,
                'filters' => [ /** Add filters **/ ],
                'validators' => [ /** Add validators **/],
            ],
        ],
        'simple-key' => [
            'required' => true,
            'filters' => [ /** Add filters **/ ],
            'validators' => [ /** Add validators **/],
        ],
    ],
],

对象集合

出于某种原因,验证对象集合的规范有点不同:

$data = [
    'root-key' => [[
        'sub-key' => 'my-value',
        'sub-key2' => 'my-other-value',
    ], [
        'sub-key' => 'my-value',
        'sub-key2' => 'my-other-value',
    ]],
    'simple-key' => 'simple-value'
];

'input_filter_specs' => [
    'my-filter' => [
        'root-key' => [
            'type' => CollectionInputFilter::class,
            'required' => true,
            'input_filter' => [
                'sub-key' => [
                    'required' => true,
                    'filters' => [ /** Add filters **/ ],
                    'validators' => [ /** Add validators **/],
                ],
                'sub-key2' => [
                    'required' => true,
                    'filters' => [ /** Add filters **/ ],
                    'validators' => [ /** Add validators **/],
                ],
            ]
        ],
        'simple-key' => [
            'required' => true,
            'filters' => [ /** Add filters **/ ],
            'validators' => [ /** Add validators **/],
        ],
    ],
],

绕过 type 限制/重新使用过滤器规范

使用 type 键,可以指定输入过滤器的类型(如前两个示例中所做的)。然而,很少有人知道指定的过滤器隐含地也是输入过滤器,并且也可以指定为类型。 这允许在其他过滤器中重复使用指定的过滤器,并由较小的过滤器组成复杂的过滤器。只需将指定输入过滤器的名称作为 type 传递即可。

$data = [
    'root-key' => [
        'sub-key' => 'my-value',
        'sub-key2' => 'my-other-value',
    ],
    'simple-key' => 'simple-value'
];

'input_filter_specs' => [
    'root-key-filter' => [
        'sub-key' => [
            'required' => true,
            'filters' => [ /** Add filters **/ ],
            'validators' => [ /** Add validators **/],
        ],
        'sub-key2' => [
            'required' => true,
            'filters' => [ /** Add filters **/ ],
            'validators' => [ /** Add validators **/],
        ],
    ],
    'my-filter' => [
        'root-key' => [
            'type' => 'root-key-filter',
        ],
        'simple-key' => [
            'required' => true,
            'filters' => [ /** Add filters **/ ],
            'validators' => [ /** Add validators **/],
        ],
    ],
],

然后这样做允许您在新创建的输入过滤器中使用 type 名称:

$data = [
    'root-key' => [
        'type' => 'my-value',
    ],
];

'input_filter_specs' => [
    'root-key-filter' => [
        'type' => [
            'required' => true,
            'filters' => [ /** Add filters **/ ],
            'validators' => [ /** Add validators **/],
        ],
    ],
    'my-filter' => [
        'root-key' => [
            'type' => 'root-key-filter',
        ],
    ],
],

我希望这个迟到的答案对那里的任何人仍然有用。威尔特的回答肯定是,并使我在这方面走上了正确的道路。