jsonschema过滤掉无效项

时间:2019-07-12 05:26:28

标签: jsonschema

我正在使用https://www.npmjs.com/package/jsonschema验证看起来像这样的传入JSON数组

{
    "source": "app",
    "data": [
        {
            "customerId": "1",
            "Address": {
                "number": "1",
                "street": "st1"
            }
        },
        {
            "customerId": "2",
            "Address": {
                "number": "2",
                "street": "st1"
            }
        },
        {
            "customerId": "3",
            "Address": {
                "number": "3"
            }
        }
    ]
}

在此数据中,第三项customerId: 3无效,因为它缺少street中的address字段。有什么办法让我找出data中已过滤的有效项目?

我尝试了errors.instance,但是例如最多只能给Address

"Address": {
                "number": "3"
            }

理想情况下,我想要的是以下

{
    "source": "app",
    "data": [
        {
            "customerId": "1",
            "Address": {
                "number": "1",
                "street": "st1"
            }
        },
        {
            "customerId": "2",
            "Address": {
                "number": "2",
                "street": "st1"
            }
        }
    ]
}

0 个答案:

没有答案