gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:56:45 24 4
gpt4 key购买 nike

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

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

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

非常感谢您的回答

最佳答案

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

嵌套对象

已由威尔特回答,为完整性添加。

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

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

关于php - 如何按照 Apigility 方式验证嵌套数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29669070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com