json复杂模式可重用定义,嵌套属性中的$ ref验证被忽略

时间:2019-05-24 16:51:10

标签: json jsonschema

尝试学习JSON模式,我有一个复杂的模式,它们全部基于相对URL。当嵌套属性调用另一个文件中定义的definitions属性时,有时会忽略验证。

我的目标是基本上在全球范围内进行可重用的定义和常用的enums / consts / definitions

假设项目树为:

-------------------------------------------
| C:/project                              |
| `----main.json (file to be validated)   |
| `----/schemas                           |
|   `----base.json                        |
|   `----datatypes.json                   |
|   `----defs.json                        |
|   `----frequent.json                    |
|   `----/pii                             |
|     `----jobs.json                       |
-------------------------------------------

基于我对$id在根级别ref的工作方式的理解,是它为$ref相对的架构设置了基本uri。但是,在某些情况下,我的架构未针对错误值进行验证,而在其他情况下,则未针对其进行验证。嵌套,否则。

datatypes.json

{
  "$id": "http://project.com/schemas/datatypes.json",
  "$schema" : "http://json-schema.org/draft-07/schema",

  "definitions" : {
    "type_number": {
      "type": "number",
      "default": 0.0
    },

    "type_integer": {
      "type": "integer",
      "default": 0
    },

    "type_nonempty_string" : {
      "type": "string",
      "minLength": 1,
      "pattern": "^([^\\s].*[^\\s]*)$"
    }
  }
}

defs.json

{
  "$id": "http://project.com/schemas/defs.json",
  "$schema": "http://json-schema.org/draft-07/schema",
  "definitions": {
    "number": {
      "$ref": "./datatypes.json#/definitions/type_number"
    },
    "null_number": {
      "oneOf": [
        {
          "$ref": "#/definitions/number"
        },
        {
          "type": "null"
        }
      ]
    },
    "positive_number": {
      "$ref": "#/definitions/number",
      "minimum": 0.0
    },
    "positive_nonzero_number": {
      "$ref": "#/definitions/number",
      "exclusiveMinimum": 0.0
    },
    "integer": {"$ref": "./datatypes.json#/definitions/type_integer"},
    "positive_integer" : {
      "$ref": "#/definitions/integer",
      "minimum" : 0
    },
    "positive_nonzero_integer" : {
      "$ref": "#/definitions/integer",
      "exclusiveMinimum" : 0
    },

    "strict_object": {
      "type": "object",
      "additionalProperties": false
    },
    "nonempty_string": {"$ref": "./datatypes.json#/definitions/type_nonempty_string"},
    "nonempty_string_null" : {
      "oneOf": [
        {
          "$ref": "#/definitions/nonempty_string"
        },
        {
          "type": "null"
        }
      ]
    }
  }
}

frequent.json

{
  "$id": "http://project.com/schemas/frequent.json",
  "$schema" : "http://json-schema.org/draft-07/schema",
  "definitions" : {
    "SEX_TYPES" : {
      "enum" : ["Male", "M","Female","F"]
    },
    "STATES" : {
      "$comment" : "assume this is an emum of states in US",
      "enum" : ["AK","AL","CT"]
    },

    "address" : {
      "$ref" : "./defs.json#/definitions/strict_object",
      "required" : ["street1", "city", "state", "zipcode"],
      "properties" : {
        "street1" : { "$ref" : "./defs.json#/definitions/nonempty_string" },
        "street2" : { "$ref" : "./defs.json#/definitions/nonempty_string" },
        "city" : { "$ref" : "./defs.json#/definitions/nonempty_string" },
        "state" : { "$ref" : "#/definitions/STATES" },
        "zipcode" : { "$ref" : "#/definitions/zipcode" }
      }
    },

    "zipcode": {
      "allOf": [
        {
          "$ref": "./defs.json#/definitions/nonempty_string"
        },
        {
          "pattern": "^(\\d{5})$"
        },
        {
          "not": {
            "pattern": "^(00000)$"
          }
        }
      ]
    }
  }
}

base.json

{
  "$id": "http://project.com/schemas/base.json",
  "$schema" : "http://json-schema.org/draft-07/schema",
  "type": "object",
  "$ref": "./defs.json#/definitions/strict_object",
  "properties": {
    "main": {
      "$ref": "./defs.json#/definitions/strict_object",
      "required": ["age", "sex", "empl_num", "description", "address", "jobs"],
      "properties": {
        "age": {"$ref": "./defs.json#/definitions/positive_nonzero_integer"},
        "sex": {"$ref": "./frequent.json#/definitions/SEX_TYPES" },
        "empl_num" : {"$ref": "./defs.json#/definitions/positive_nonzero_integer"},
        "description": {"$ref": "./defs.json#/definitions/nonempty_string"},
        "jobs" : {"$ref": "pii/jobs.json"},
        "address": {"$ref": "./frequent.json#/definitions/address"}
      }
    }
  }
}

pii/jobs.json

{
  "$id" : "http://project.com/schemas/pii/jobs.json",
  "$schema": "http://json-schema.org/draft-07/schema",
  "type" : "array",
  "minItems": 1,
  "items": {
    "$ref" : "../defs.json#/definitions/strict_object",
    "required": ["description","company","pay_details"],
    "properties" : {
      "description" : { "$ref" : "../defs.json#/definitions/nonempty_string" },
      "company" : {
        "$ref": "../defs.json#/definitions/strict_object",
        "required": ["name","address"],
        "properties": {
          "name" : { "$ref" :  "../defs.json#/definitions/nonempty_string" },
          "address" : {"$ref":  "../frequent.json#/definitions/address"}
        }
      },
      "pay_details" : {
        "$ref" : "../defs.json#/definitions/strict_object",
        "required": ["pay_grade", "salary", "performance_review"],
        "properties": {
          "pay_grade" : { "$ref" : "../defs.json#/definitions/positive_nonzero_integer" },
          "salary" : { "$ref" : "../defs.json#/definitions/positive_nonzero_number" },
          "performance_review" : {
            "$ref" : "../defs.json#/definitions/strict_object",
            "required" : ["previous", "nextDate", "numberOfWriteUps"],
            "properties" : {
              "notes" : { "$ref" : "../defs.json#/definitions/nonempty_string" },
              "numberOfWriteUps" : {"$ref" : "../defs.json#/definitions/positive_integer" },
              "previous" : {
                "$ref" : "../defs.json#/definitions/strict_object",
                "required": ["date", "rating"],
                "properties": {
                  "date" : {
                    "$ref" : "../defs.json#/definitions/nonempty_string",
                    "format" : "date-time"
                  },
                  "rating" : {"$ref" : "../defs.json#/definitions/positive_integer" }
                }
              },
              "nextDate" : {
                "$ref" : "../defs.json#/definitions/nonempty_string",
                "format" : "date-time"
              }
            }
          }
        }
      }
    }
  }
}

在intellij中,结果如下:(每个显示的属性都不应该是有效的,但是有些属性只是被忽略和允许的(例如负数和null等)。我不会收到有关$ ref不能的警告/错误自动完成甚至可以为我完成相对文件名和定义属性。

pic1


修改

更多地使用它之后,我发现它与相对URL有关。例如,在我的pii(个人身份信息)文件夹中,作业模式位于该文件夹中,如果我将该模式与其他模式一起移至根目录,则所有内容都将得到正确验证。为了说明新的项目树:

-------------------------------------------
| C:/project                              |
| `----main.json (file to be validated)   |
| `----/schemas                           |
|   `----base.json                        |
|   `----datatypes.json                   |
|   `----defs.json                        |
|   `----frequent.json                    |
|   `----jobs.json                        |
-------------------------------------------

pic2

为简便起见,我的架构实际上并未存储在http://project.com中。根据我对$id的理解,这无关紧要,这只是架构相互通信的一种方式。我想尽可能地将其模块化,然后将模式分为各自的子文件夹(例如pii),但是在我的示例中,它并不能以此方式进行验证。

1 个答案:

答案 0 :(得分:1)

看起来您走在正确的轨道上,但是您不能将$ref与同一对象中的其他关键字一起使用,因为当将$ref与草稿7 JSON模式一起使用时,它们总是被忽略。 / p>

  

8.3。具有“ $ ref”的模式引用

     

“ $ ref”关键字用于引用模式,并提供   通过自我引用验证递归结构的能力。

     

具有“ $ ref”属性的对象模式必须解释为
  “ $ ref”参考。 “ $ ref”属性的值必须是URI
  参考。根据当前URI基础进行解析,它标识了
  要使用的架构的URI。 “ $ ref”对象中的所有其他属性必须   被忽略。

https://tools.ietf.org/html/draft-handrews-json-schema-01#section-8.3

如果要在同一架构对象中将$ref与其他关键字一起使用,则需要创建两个架构对象并将它们包装在allOf中。