代码之家  ›  专栏  ›  技术社区  ›  Seegel

调用Sub Shema以验证Json Schema中的不同属性

  •  0
  • Seegel  · 技术社区  · 2 年前

    我正在准备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"
                    }
                }
                
            }
            
        ],
    
    }
    

    我想用不同的方式验证每个任务,如果任务类型是User,它将具有不同的属性,如果是Manager,它将拥有不同的属性。我想将通用属性分组到一个模式中,然后为每个任务中的属性创建不同的模式。

    下面是主模式的伪代码

    if type ="User"
       properties = $ref : #/definitions/UserProperties
    if type ="Manager"
        properties = $ref : #/definitions/ManagerProperties
    if type ="Director"
        properties = $ref : #/definitions/DirectorProperties
    

    我定义的子模式的所有这些定义。但是验证器并没有验证子模式。

    每个子模式都将验证需要或不需要什么属性。

    知道如何根据任务类型值进行调用吗?如果类型为user,则验证用户模式;如果类型为manager,则验证manager模式。

    0 回复  |  直到 2 年前
        1
  •  1
  •   Jeremy Fiel    2 年前

    您可以使用 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"
                  }
              }
              
          }
          
      ]
    
    }