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

时间:2015-04-16 08:17:07

标签: php json zend-framework2 apigility

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

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

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

非常感谢您的答案

2 个答案:

答案 0 :(得分:5)

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

嵌套对象

威尔特已经回答,补充说完整性。

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

我希望这个迟到的答案对那里的任何人都有用。威尔茨回答肯定是,并把我带到了正确的轨道上。

答案 1 :(得分: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'
    )
);

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