[英]Optional Json validation using tv4 - Javascript
我正在嘗試使用tv4
驗證我的 JSON 架構。 它正在工作並且驗證返回True
。
但是,在我的情況下,JSON 系列"first, second, and third"
將不會一直可用。
在這種情況下如何編寫模式?
我的 JSON 數據
{
"checked": "OK",
"result": {
"first": {
"label": "First Label",
"value": 1
},
"second": {
"label": "second Label",
"value": 34
},
"third": {
"label": "Third Label",
"value": 28
}
}
}
JSON 架構
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "http://example.com/example.json",
"type": "object",
"title": "The root schema",
"description": "The root schema comprises the entire JSON document.",
"default": {},
"required": [
"checked",
"result"
],
"properties": {
"checked": {
"$id": "#/properties/checked",
"type": "string",
"title": "The checked schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"OK"
]
},
"result": {
"$id": "#/properties/result",
"type": "object",
"title": "The result schema",
"description": "An explanation about the purpose of this instance.",
"default": {},
"required": [
"first",
"second",
"third"
],
"properties": {
"first": {
"$id": "#/properties/result/properties/first",
"type": "object",
"title": "The first schema",
"description": "An explanation about the purpose of this instance.",
"default": {},
"examples": [
{
"label": "First Label",
"value": 1
}
],
"required": [
"label",
"value"
],
"properties": {
"label": {
"$id": "#/properties/result/properties/first/properties/label",
"type": "string",
"title": "The label schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"First Label"
]
},
"value": {
"$id": "#/properties/result/properties/first/properties/value",
"type": "integer",
"title": "The value schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
1
]
}
},
"additionalProperties": true
},
"second": {
"$id": "#/properties/result/properties/second",
"type": "object",
"title": "The second schema",
"description": "An explanation about the purpose of this instance.",
"default": {},
"examples": [
{
"label": "second Label",
"value": 34
}
],
"required": [
"label",
"value"
],
"properties": {
"label": {
"$id": "#/properties/result/properties/second/properties/label",
"type": "string",
"title": "The label schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"second Label"
]
},
"value": {
"$id": "#/properties/result/properties/second/properties/value",
"type": "integer",
"title": "The value schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
34
]
}
},
"additionalProperties": true
},
"third": {
"$id": "#/properties/result/properties/third",
"type": "object",
"title": "The third schema",
"description": "An explanation about the purpose of this instance.",
"default": {},
"examples": [
{
"label": "Third Label",
"value": 28
}
],
"required": [
"label",
"value"
],
"properties": {
"label": {
"$id": "#/properties/result/properties/third/properties/label",
"type": "string",
"title": "The label schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"Third Label"
]
},
"value": {
"$id": "#/properties/result/properties/third/properties/value",
"type": "integer",
"title": "The value schema",
"description": "An explanation about the purpose of this instance.",
"default": 0,
"examples": [
28
]
}
}
}
}
}
}
}
在 JSON Schema 中,默認情況下所有屬性都是可選的。 您的架構根據需要顯式聲明這些屬性。 要使它們成為可選,請刪除該關鍵字: "required": ["first", "second", "third"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.