简体   繁体   English

Json模式,以根据另一个对象的内容验证对象的值

[英]Json schema to validate object's values against content of another object

I'm trying to create json schema for a document where field values in some object should validate against a enum defined in another object in the same document. 我正在尝试为文档创建json模式,其中某个对象中的字段值应该针对同一文档中另一个对象中定义的枚举进行验证。

More specifically, in the example below, I'd like to be able to define "properties" with id and values (I should be able to define different properties in different json files). 更具体地说,在下面的示例中,我希望能够使用idvalues定义“属性”(我应该能够在不同的json文件中定义不同的属性)。 Then "objects" should be able to refer to these properties, so that object.properties[i].id must match with id of one of the properties and object.properties[i].value must match with one of the enum values defined for that property. 然后“objects”应该能够引用这些属性,这样object.properties[i].id必须与其中一个属性的id匹配,而object.properties[i].value必须与其中一个枚举值匹配对于那个属性。

{
    "properties": [
        {
            "id": "SIZE",
            "values": ["small", "medium", "big"]
        },
        {
            "id": "MATERIAL",
            "values": ["wood", "glass", "steel", "plastic"]
        },
        {
            "id": "COLOR",
            "values": ["red", "green", "blue"]
        }
    ],

    "objects": [
        {
            "name": "chair",
            "properties": [
                {
                    "id": "SIZE",
                    "value": "small"
                },
                {
                    "id": "COLOR",
                    "value": "red"
                }
            ],
        },
        {
            "name": "table",
            "properties": [
                {
                    "id": "MATERIAL",
                    "value": "wood"
                }
            ]
        }
    ]
}

I tried to create json schema to validate such structure, but got stuck with describing reference to inner fields of "property" object. 我试图创建json模式来验证这种结构,但是在描述对“属性”对象的内部字段的引用时却陷入困境。 I also looked into the standard and did not find a way to achieve the goal. 我也调查了标准 ,没有找到实现目标的方法。

Is it possible to create a json schema which would validate my json files? 是否可以创建一个json模式来验证我的json文件?

There is a proposal for $data reference that almost allows to do it if you change your data structure a little bit to remove one level of indirection. 有一个$ data引用的提议,如果你稍微改变你的数据结构以删除一个级别的间接,几乎允许这样做。 It's is supported in Ajv (I am the author). Ajv支持它(我是作者)。

So if your data were: 因此,如果您的数据是:

{
    "properties": {
        "SIZE": ["small", "medium", "big"],
        "MATERIAL": ["wood", "glass", "steel", "plastic"],
        "COLOR": ["red", "green", "blue"]
    },
    "objects": {
        "chair": {
            "SIZE": "small",
            "COLOR": "red"
        },
        "table": {
            "MATERIAL": "wood"
        }
    }
}

then your schema could have been: 那么你的架构可能是:

{
    "type": "object",
    "properties": {
        "properties": {
            "type": "object",
            "additionalProperties": {
                "type": "array",
                "items": { "type": "string" }
            } 
        },
        "objects": {
            "type": "object",
            "additionalProperties": {
                "type": "object",
                "properties": {
                    "SIZE": {"enum": {"$data": "3/properties/SIZE"}},
                    "MATERIAL": {"enum": {"$data": "3/properties/MATERIAL"}},
                    "COLOR": {"enum": {"$data": "3/properties/MATERIAL"}}
                }
            }
        }
    }
}

And it could be dynamically generated based on all list of possible properties. 它可以根据所有可能的属性列表动态生成。

With the data structure you have you either can use custom keywords if the validator supports them or implement some part of validation logic outside of JSON schema. 使用数据结构,如果验证器支持自定义关键字,或者在JSON模式之外实现验证逻辑的某些部分,您可以使用自定义关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM