简体   繁体   English

嵌套必需属性的JSON模式的适当行为?

[英]Appropriate behavior of JSON schema for nested required properties?

Say I have a JSON schema like this: 说我有一个像这样的JSON模式:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "myKey": {"$ref": "myKey.json#"}
    },
    "additionalProperties": false
}

and then somewhere else I have myKey.json: 然后在其他地方我有myKey.json:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object"
    "properties": {
        "A": {
            "type": "array",
            "description": "Array of stream object IDs",
            "items": { "type": "integer" }
        },
        "B": {
            "type": "array",
            "description": "Array of stream object IDs",
            "items": {"type": "integer" }
        }
    },
    "required": ["A", "B"],
    "additionalProperties": false
}

The important thing here is that inside myKey, there are two required properties, but myKey itself is not required. 这里重要的是,在myKey中,有两个必需的属性,但是myKey本身不是必需的。 Does the fact that myKey have required properties propagate up to the top so that myKey is forced to become required? myKey具有必需属性的事实是否会传播到顶部,从而迫使myKey被强制成为必需的? In other words, which of these two objects, if any, ought to be validated by this schema? 换句话说,该模式应验证这两个对象中的哪一个(如果有)?

{
  "name": "myName",
}

{
  "name": "myOtherName",
  "myKey": 
   {
     "A": [1, 2]      // Note that B is missing
   }
}

The first one is valid according to the schema and the second one no. 根据模式,第一个有效,而第二个则无效。

The way to read properties tag is: if this property key is found, then it must satisfy this schema. 读取属性标签的方法是:如果找到了此属性键,则它必须满足此架构。

{
  "name": "myName"
}

For the object above, myKey is not required so it satisfies the schema. 对于上面的对象, 不需要myKey,因此它满足架构。

{
  "name": "myOtherName",
  "myKey": 
   {
     "A": [1, 2]      // Note that B is missing
   }
}

For the second object, myKey is present, so it must satisfy the schema of that property. 对于第二个对象,存在myKey ,因此它必须满足该属性的架构。 But it is not satisfied because it should have both A and B properties. 但这并不令人满意,因为它应该同时具有AB属性。

The same idea is applied to every level. 相同的想法适用于每个级别。 The following object satisfies the schema: 以下对象满足该架构:

{
    "name": "myOtherName",
    "myKey": 
    {
        "A": [],
        "B": []
    }
}

But this does not: 但这不是:

{
    "name": "myOtherName",
    "myKey": 
    {
        "A": [],
        "B": ""
    }
}

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

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