如果这个问题令人困惑,我很抱歉。我正在尝试解决jsonschema验证问题。我试图解决的问题是将属性设置为可选。该属性属于必需的对象,而该对象又是复杂类型的属性。
例如:
{ "prop1" : {
              "field1" : {"type" : "string" },
              "field2" : {"type" : "string" }
           },
  "prop2" : { "type" : "string" }
}如果我想声明prop1是必需的,但是pro1.field1是可选的,我该如何使用jsonschema来实现呢?
谢谢,罗宾
发布于 2018-03-06 02:31:32
您需要在"properties“的同一级别指定"required”。
{
  "type": "object",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "properties": {
    "prop1": {
      "type": "object",
      "properties": {
        "field1": {
          "type": "object",
          "properties": {
            "type": {
              "type": "string"
            }
          }
        },
        "field2": {
          "type": "object",
          "properties": {
            "type": {
              "type": "string"
            }
          }
        }
      }
    },
    "prop2": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string"
        }
      }
    }
  },
  "required" : [ "prop1" ]
}此模式是使用此在线工具(外加一些编辑)生成的:
https://stackoverflow.com/questions/49091048
复制相似问题