您可以使用
if, then
JSON Schema draft-07或更高版本中的语法
properties
中的属性
tasks
大堆
我定义了一个基本
taskType
每个都具有共享属性和单独的模式
type
.(用户、经理、主管)
这个
如果,那么
语法用于
taskType
在其中检查的值
类型
并定义了正确的模式。我们使用
allOf
因为我们有多个
如果,那么
要验证的条件。如果定义
类型
则应用正确的模式。如果没有
类型
如果已识别,则在
特性
因此,任何事情都是有效的。
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"orderName": {
"type": "string"
},
"orderType": {
"type": "string"
},
"requestId": {
"type": "string"
},
"tasks": {
"type": "array",
"uniqueItems": true,
"items": {
"$ref": "#/$defs/taskType"
}
}
},
"$defs": {
"taskType": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"taskId": {
"type": "integer"
},
"name": {
"type": "string"
},
"executor": {
"type": "string"
},
"description": {
"type": "string"
},
"properties": {}
},
"allOf": [
{
"if": {
"properties": {
"type": {
"const": "User"
}
}
},
"then": {
"properties": {
"properties": {
"$ref": "#/$defs/UserTask"
}
}
}
},
{
"if": {
"properties": {
"type": {
"const": "Manager"
}
}
},
"then": {
"properties": {
"properties": {
"$ref": "#/$defs/ManagerTask"
}
}
}
},
{
"if": {
"properties": {
"type": {
"const": "Director"
}
}
},
"then": {
"properties": {
"properties": {
"$ref": "#/$defs/DirectorTask"
}
}
}
}
]
},
"UserTask": {
"type": "object",
"properties": {
"stateId": {
"type": "integer"
},
"params": {
"type": "array",
"uniqueItems": true,
"items": {
"type": "string"
}
}
},
"additionalProperties": false
},
"ManagerTask": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"baseId": {
"type": "string"
},
"method": {
"type": "string"
},
"params": {
"type": "object",
"properties": {
"Id": {
"type": "string"
},
"tag": {
"type": "string"
}
}
},
"locale": {
"type": "object",
"propertyNames": {
"pattern": "^[a-zA-Z]+$"
},
"patternProperties": {
"": {
"type": "string"
}
}
}
},
"additionalProperties": false
},
"DirectorTask": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"payload": {
"type": "string"
},
"Size": {
"type": "string"
},
"State": {
"type": "object",
"propertyNames": {
"pattern": "^[A-Z]{2}$"
},
"patternProperties": {
"": {
"type": "string"
}
}
}
},
"additionalProperties": false
}
}
}
{
"orderName": "CoreCapital",
"orderType": "Capital",
"requestId": "Generated ID",
"tasks": [
{
"type": "User",
"taskId": 1,
"name": "Level-1",
"executor": "Marine",
"description": "Get Details",
"properties": {
"stateId": 123,
"params": [
"parameter1",
"parameter2"
]
}
},
{
"type": "Manager",
"taskId": 2,
"name": "Lavel-2",
"executor": "Shipping",
"description": "Shipping Details",
"properties": {
"id": 2345,
"baseId": "Shipping-23",
"method": "POST",
"params": {
"Id": "1234",
"tag": "Fire"
},
"locale": {
"USA": "tag-1",
"Canada": "tag-2",
"Europe": "tag-3"
}
}
},
{
"type": "Director",
"taskId": 3,
"name": "Lavel-3",
"executor": "Transport",
"description": "Transport Details",
"properties": {
"name" : "Water-resource",
"payload": "truck details",
"Size": "40 ton",
"State": {
"CA": "tag-4",
"AR": "tag-5",
"MS": "tag-6"
}
}
}
]
}