JSON 架构:类型中的架构
JSON schema: schemas in types
我正在尝试创建一个复杂的 JSON 模式,该模式试图在不访问 OneOf、AnyOf 等的情况下使用条件依赖关系
我基本上是在尝试结合
const schema1 = {
type: "object",
properties: {
q1: {
type: "boolean",
enum: [false]
}
},
required: ["q1"]
}
和
const schema2 = {
type: "object",
properties: {
q1: {
type: "boolean",
enum: [true]
}
sq1: {
type: "boolean"
}
},
required: ["q1", "sq1"]
}
到一个模式 combined_schema
从而模拟条件依赖,如果 q1
的答案为真,则需要 sq1
的答案。
在 JSON schema wiki 中,我读到 AnyOf 将替换类型中的 "schema"”但是看着这个例子我不确定在特定情况下如何使用它(The {"schema1": "here"} 部分非常混乱。
https://github.com/json-schema/json-schema/wiki/anyOf,-allOf,-oneOf,-not
有人可以帮我将 wiki 示例应用到我的现实世界问题吗?
我找到了答案。他们的关键是使用 refs
{
"type": [
{"$ref": "#/schema1"},
{"$ref": "#/schema2"}
],
"schema2":{
"type": "object",
"properties": {
"q1": {
"type": "boolean",
"enum": [true]
},
"sq1": {
"type": "boolean"
}
},
"required": ["q1", "sq1"]
},
"schema1": {
"type": "object",
"properties": {
"q1": {
"type": "boolean",
"enum": [false]
}
},
"required": ["q1"]
}
}
您回答的架构不是有效的 JSON-架构。您可以使用 anyOf 关键字来完成:
{
type: "object",
required: ["q1"]
anyOf: [
{
properties: {
q1: { enum: [false] } // no need for type here
}
},
{
properties: {
q1: { enum: [true] },
sq1: { type: "boolean" }
},
required: ["sq1"]
}
]
}
还有关键字switch from JSON-Schema v5 proposals implemented in Ajv(免责声明:我创建的)。
我正在尝试创建一个复杂的 JSON 模式,该模式试图在不访问 OneOf、AnyOf 等的情况下使用条件依赖关系
我基本上是在尝试结合
const schema1 = {
type: "object",
properties: {
q1: {
type: "boolean",
enum: [false]
}
},
required: ["q1"]
}
和
const schema2 = {
type: "object",
properties: {
q1: {
type: "boolean",
enum: [true]
}
sq1: {
type: "boolean"
}
},
required: ["q1", "sq1"]
}
到一个模式 combined_schema
从而模拟条件依赖,如果 q1
的答案为真,则需要 sq1
的答案。
在 JSON schema wiki 中,我读到 AnyOf 将替换类型中的 "schema"”但是看着这个例子我不确定在特定情况下如何使用它(The {"schema1": "here"} 部分非常混乱。
https://github.com/json-schema/json-schema/wiki/anyOf,-allOf,-oneOf,-not
有人可以帮我将 wiki 示例应用到我的现实世界问题吗?
我找到了答案。他们的关键是使用 refs
{
"type": [
{"$ref": "#/schema1"},
{"$ref": "#/schema2"}
],
"schema2":{
"type": "object",
"properties": {
"q1": {
"type": "boolean",
"enum": [true]
},
"sq1": {
"type": "boolean"
}
},
"required": ["q1", "sq1"]
},
"schema1": {
"type": "object",
"properties": {
"q1": {
"type": "boolean",
"enum": [false]
}
},
"required": ["q1"]
}
}
您回答的架构不是有效的 JSON-架构。您可以使用 anyOf 关键字来完成:
{
type: "object",
required: ["q1"]
anyOf: [
{
properties: {
q1: { enum: [false] } // no need for type here
}
},
{
properties: {
q1: { enum: [true] },
sq1: { type: "boolean" }
},
required: ["sq1"]
}
]
}
还有关键字switch from JSON-Schema v5 proposals implemented in Ajv(免责声明:我创建的)。