试图学习 JSON 模式,我有一个复杂的模式,它都是基于相对 URL 的。当嵌套属性调用另一个文件中定义的定义属性时,验证有时会被忽略。
我的目标是基本上在全球范围内制作可重用的定义和常用的枚举/常量/定义
假设项目树是:
-------------------------------------------
| C:/project |
| `----main.json (file to be validated) |
| `----/schemas |
| `----base.json |
| `----datatypes.json |
| `----defs.json |
| `----frequent.json |
| `----/pii |
| `----jobs.json |
-------------------------------------------
根据我对$id
根级别ref的工作原理的理解,它为模式设置了基本 uri,$ref
相对于哪里。但是,在某些情况下,我的架构未针对错误值进行验证,而其他情况下。嵌套,或以其他方式。
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 中,结果如下:(显示的每个属性都不应该是有效的,但是有些属性被简单地忽略和允许(如负数和空值等)。我没有收到关于 $ref 无法解决的警告/错误. 自动完成甚至为我完成了相对文件名和定义属性。
编辑
在玩了一些之后,我发现它与相对 URL 有关。例如,在我的 pii(个人身份信息)文件夹中,作业模式所在的位置,如果我将该模式与其他模式一起移动到根目录,那么一切都会得到正确验证。为了说明新的项目树:
-------------------------------------------
| C:/project |
| `----main.json (file to be validated) |
| `----/schemas |
| `----base.json |
| `----datatypes.json |
| `----defs.json |
| `----frequent.json |
| `----jobs.json |
-------------------------------------------
为简洁起见,我的架构实际上并未存储在http://project.com
. 根据我$id
对这无关紧要的理解,这只是模式相互通信的一种方式。我想尽可能地模块化并将模式分离到它们自己的子文件夹(如 pii)中,但是在我的示例中它并没有验证这种方式。