jsonschema filter out invalid items

不问归期 提交于 2019-12-11 19:45:49

问题


I'm using https://www.npmjs.com/package/jsonschema to validate an incoming JSON array that looks something like this

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

In this data the third item customerId: 3 is invalid since it's missing the street field in address. Is there any way for me to get out the filtered items in data that are valid?

I tried errors.instance, but it gives me only up to Address for example

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

Ideally what I want out is the following

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

来源:https://stackoverflow.com/questions/57000651/jsonschema-filter-out-invalid-items

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!