json - "required"JSON 模式中的关键字

标签 json jsonschema

我从 http://json-schema.org/examples.html 得到了以下架构,我想知道所需的关键字是否只能出现在顶层。或者如果有对象类型的属性,它也可以包含在属性中。我在规范中找不到与此相关的任何内容 https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00#section-5.4.3 .

{
    "title": "Example Schema",
    "type": "object",
    "properties": {
        "firstName": {
            "type": "string"
        },
        "lastName": {
            "type": "string"
        },
        "age": {
            "description": "Age in years",
            "type": "integer",
            "minimum": 0
        }
    },
    "required": ["firstName", "lastName"]
}

所以下面的例子是一个有效的模式

{  
   "title":"Example Schema",
   "type":"object",
   "properties":{  
      "firstName":{  
         "type":"string"
      },
      "lastName":{  
         "type":"string"
      },
      "age":{  
         "type":"object",
         "properties":{  
            "minAge":{  
               "type":"number"
            },
            "maxAge":{  
               "type":"number"
            },
            "required":[  
               "minAge",
               "maxAge"
            ]
         }
      }
   },
   "required":[  
      "firstName",
      "lastName"
   ]
}

最佳答案

4.4 Keywords with the possibility to validate container instances (arrays or objects) only validate the instances themselves and not their children (array items or object properties).

所以我看到是的,你可以在任何级别上进行验证,但验证应该只在需要的同一级别上考虑

关于json - "required"JSON 模式中的关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29389583/

相关文章:

javascript - $.post(...) 无法识别 JSON 主体

php - 在 composer.json 中运行命令行命令

javascript - Node js 服务器 : I want to respond to a browser request with both HTML and JSON in the same response, 这可能吗?

json - Spark : Use JsonSchema to parse Json into DataFrame

使用 Validator(或类似的东西)进行 python 数据结构验证

jsonschema - 如何在 JSON 模式验证器中表示联合类型?

java - 使用 JSON 文件填充 ListView

python - 使用 Python 从 REST API 获取大型分页数据

json - JSON 模式中是否可以有一个动态大小的数组,其大小基于另一个值

json - 您应该如何引用 JSON 对象符合的 JSON 模式?