我有以下示例模式:
{
"id": "http://schema.acme.com/widgets",
"$schema": "http://json-schema.org/draft-06/schema#",
"definitions": {
"bentWidget": {
"type": "object",
"required": ["angle", "baseWidget"],
"properties": {
"angle": {
"type": "number",
"minimum": 0,
"maximum": 90
},
"baseWidget": {
"$ref": "#/definitions/baseWidget"
}
}
},
"highPowerWidget": {
"type": "object",
"required": ["power", "baseWidget"],
"properties": {
"power": {
"type": "number",
"minimum": 101,
"maximum": 200
},
"baseWidget": {
"$ref": "#/definitions/baseWidget"
}
}
},
"color": {
"description": "the color of a widget",
"type": "string"
},
"baseWidget": {
"description": "The base type for a widget",
"type": "object",
"required": [
"title",
"version",
"colors"
],
"properties": {
"title": {
"type": "string",
"maximum": 100,
"minimum": 1,
"pattern": "^[a-zA-Z]+((_[a-zA-Z]+)*|([a-zA-Z]+_)*|_)"
},
"flanged": {
"type": "boolean"
},
"version": {
"type": "string",
"maximum": 64,
"minimum": 1
},
"colors": {
"type": "array",
"items": {
"$ref": "#/definitions/color"
}
}
}
}
},
"anyOf": [
{ "$ref": "#/definitions/baseWidget" },
{ "$ref": "#/definitions/bentWidget" },
{ "$ref": "#/definitions/highPowerWidget" }
]
}
Run Code Online (Sandbox Code Playgroud)
我想测试它,所以我把它写到一个文件:
{
"type": "highPowerWidget",
"title": "foobar",
"version": "foo"
}
Run Code Online (Sandbox Code Playgroud)
然后我从shell运行ajv
$ ajv -s widgetSchema.json -d widget-highPower.json
widget-highPower.json valid
Run Code Online (Sandbox Code Playgroud)
它告诉我它是有效的,这是不正确的,highPowerWidget必须具有power属性,以及"继承"版本和colors属性.
我能够通过删除anyOf部分并输入以下内容来测试我的个人模式:
"properties": {
"testObject": {
"type": "object",
"oneOf": [
{ "$ref": "#/definitions/baseWidget" },
{ "$ref": "#/definitions/bentWidget" },
{ "$ref": "#/definitions/highPowerWidget" }
]
}
},
"required": [ "testObject" ]
Run Code Online (Sandbox Code Playgroud)
并验证此JSON:
{
"testObject": {
"type" : "highPowerWidget",
"title" : "title",
"version" : "baz",
"colors" : [ "red", "green", "blue"],
"flanged" : true
}
}
Run Code Online (Sandbox Code Playgroud)
但出于两个原因,这似乎是错误的.
我想我对如何编写或至少测试我的模式有一个基本的误解.我的目标是拥有一组全部验证的对象,以及一组都失败的文件.这样我可以对我的架构进行单元测试.
您的架构有效。正如所写,这将是一个有效的 highPowerWidget:
{
"power": 150,
"baseWidget":{
"title": "SuperHighPower",
"flanged":true,
"version": "version 1",
"colors":["blue"]
}
}
Run Code Online (Sandbox Code Playgroud)
我认为误解是 JSONSchema 不支持OO 风格继承。
相反,此模式本质上要求 highPowerWidget 或 BentWidget 上的“baseWidget”属性。 这是 AJV 的沙箱 plnkr,您可以用它来测试它。
编辑:只要想一想,您就可以完成此操作,从bentWidget和highPowerWidget中删除baseWidget属性,然后添加"allOf":[{"$ref":"#/definitions/baseWidget"}]到每个子小部件中。这将要求他们传递 baseWidget 的架构。所以你的架构将是:
{
"id": "http://schema.acme.com/widgets",
"$schema": "http://json-schema.org/draft-06/schema#",
"definitions": {
"bentWidget": {
"type": "object",
"required": ["angle"],
"properties": {
"angle": {
"type": "number",
"minimum": 0,
"maximum": 90
}
},
allOf: [{
"$ref": "#/definitions/baseWidget"
}]
},
"highPowerWidget": {
"type": "object",
"required": ["power"],
"properties": {
"power": {
"type": "number",
"minimum": 101,
"maximum": 200
}
},
allOf: [{
"$ref": "#/definitions/baseWidget"
}]
},
"color": {
"description": "the color of a widget",
"type": "string"
},
"baseWidget": {
"description": "The base type for a widget",
"type": "object",
"required": [
"title",
"version",
"colors"
],
"properties": {
"title": {
"type": "string",
"maximum": 100,
"minimum": 1,
"pattern": "^[a-zA-Z]+((_[a-zA-Z]+)*|([a-zA-Z]+_)*|_)"
},
"flanged": {
"type": "boolean"
},
"version": {
"type": "string",
"maximum": 64,
"minimum": 1
},
"colors": {
"type": "array",
"items": {
"$ref": "#/definitions/color"
}
}
}
}
},
"anyOf": [{
"$ref": "#/definitions/baseWidget"
}, {
"$ref": "#/definitions/bentWidget"
}, {
"$ref": "#/definitions/highPowerWidget"
}]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
392 次 |
| 最近记录: |