我正在准备 JSON 模式来验证我的数据。但是,无法做我正在寻找的某些事情。
My Data:
{
"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"
}
}
}
],
}
我想以不同的方式验证每个任务,如果任务类型是用户,它将有不同的属性,如果是经理,它将有不同的属性。我想将公共属性分组到一个架构中,然后为每个任务中的属性创建不同的架构。
下面是主模式的伪代码
if type ="User"
properties = $ref : #/definitions/UserProperties
if type ="Manager"
properties = $ref : #/definitions/ManagerProperties
if type ="Director"
properties = $ref : #/definitions/DirectorProperties
我定义的所有这些子模式的定义。但验证器并未验证子模式。
每个子模式将验证需要或不需要什么属性。
知道如何根据任务类型值调用吗?如果类型是用户,则验证用户模式,如果类型是经理,则像这样验证经理模式。
您可以在
if, then
数组中的 properties
属性上使用 JSON 架构草案 07 或更高版本中的 tasks
语法。
我定义了一个基本的
taskType
,其中包含每个 type
的共享属性和单独的模式。 (用户、经理、总监)
if, then
语法用于 taskType
模式,它检查 type
的值并定义正确的模式。我们使用 allOf
是因为我们有多个 if, then
条件需要验证。如果识别出定义的 type
,则应用正确的模式。如果没有识别出 type
,则 properties
上没有定义任何约束,因此,任何内容都是有效的。
{
"$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"
}
}
}
]
}