覆盖继承的 json 架构
Override inherited json schema
如何覆盖由“allOf”关键字继承的 json 架构中定义的验证规则?
示例:
{
"$schema": "http://json-schema.org/draft-06/schema",
"title": "My JSON Schema",
"description": "",
"definitions": {
"a": {
"type": "object",
"properties": {
"b": {
"type": "object",
"properties": {
"c": {
"type": "string",
"minLength": 1,
"maxLength": 100
}
},
"required": [
"c"
]
}
},
"required": [
"b"
]
}
},
"properties": {
"main": {
"type": "object",
"allOf": [
{
"$ref": "#/definitions/a"
}
]
},
"sub": {
"type": "object",
"allOf": [
{
"$ref": "#/definitions/a"
}
]
}
}
}
json 模式定义了两个对象:
- 主要
- 子
两个对象都从定义的对象“a”继承了它们的属性
但是对象“sub”应该有 属性 b.c 的其他验证规则(目前是 minLength 1 和 maxLength 100)。
所以当然下面的json是无效的:
{
"main" :{
"b": {
"c": "This property has a min length"
}
},"sub" : {
"b": {
"c": ""
}
}
}
如何覆盖 属性 b.c 的验证规则?
JSON 架构规范目前无法做到这一点。
如何覆盖由“allOf”关键字继承的 json 架构中定义的验证规则?
示例:
{
"$schema": "http://json-schema.org/draft-06/schema",
"title": "My JSON Schema",
"description": "",
"definitions": {
"a": {
"type": "object",
"properties": {
"b": {
"type": "object",
"properties": {
"c": {
"type": "string",
"minLength": 1,
"maxLength": 100
}
},
"required": [
"c"
]
}
},
"required": [
"b"
]
}
},
"properties": {
"main": {
"type": "object",
"allOf": [
{
"$ref": "#/definitions/a"
}
]
},
"sub": {
"type": "object",
"allOf": [
{
"$ref": "#/definitions/a"
}
]
}
}
}
json 模式定义了两个对象:
- 主要
- 子
两个对象都从定义的对象“a”继承了它们的属性 但是对象“sub”应该有 属性 b.c 的其他验证规则(目前是 minLength 1 和 maxLength 100)。
所以当然下面的json是无效的:
{
"main" :{
"b": {
"c": "This property has a min length"
}
},"sub" : {
"b": {
"c": ""
}
}
}
如何覆盖 属性 b.c 的验证规则?
JSON 架构规范目前无法做到这一点。