2

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

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

some_property为这个嵌套数据设置过滤器和验证器的正确方法是another_property什么?

非常感谢您的回答

4

2 回答 2

8

I know this answer is quite late. I stumbled on the same issue (unrelated to Apigility). After a lot of try & error I found a fully working InputFilter specification on validating nested fields / collections as well as keys named type. Adding this here for reference should others find this (hello future me).

Nested Objects

Already answered by Wilt, adding for completeness.

$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 **/],
        ],
    ],
],

Collections of Objects

For some reason the specification for validating a collection of objects is a bit different:

$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 **/],
        ],
    ],
],

Bypassing the type limitation / re-using filters specifications

Using the type key, the type of input filter can be specified (as done in the two examples before). What few do know however is that the specified filters implicitly are input filters as well and can be specified as the type too. This allows re-using specified filters within other filters and composing complex filters out of smaller ones. Simply pass the name of the specified input filter as the 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 **/],
        ],
    ],
],

Doing so then allows you to use the type name in the newly created input filter:

$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',
        ],
    ],
],

I hope this late answer is still useful for anyone out there. Wilts answer sure was and brought me on the right track on this.

于 2017-03-09T11:23:37.383 回答
2

您可以在过滤器和验证器配置中设置嵌套数据,就像您通常在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'
    )
);

查看更多信息以及如何在此处查看 StackOverfow 上的另一个问题以及如何配置 ZF 内容验证或此处的Content-Validation 模块文档

于 2015-04-16T12:25:30.257 回答