【问题标题】:Need to validate Json data against Json Schema需要针对 Json Schema 验证 Json 数据
【发布时间】:2021-10-21 21:10:53
【问题描述】:

验证架构时出现以下错误 -

无法读取 JSON 架构:virtual://server/schema.json 解析架构引用“JsonValidate#/process.schema.json”时出错。路径“properties.configurationPayload.properties.processPayload”,第 17 行,位置 35。

JsonSchema -

{
    "$schema": "http://json-schema.org/schema#",
    "title": "Activity Payload",
    "description": "A json payload used to supply configuration to Data Factory activities.",
    "type": "object",
    "properties": {
        "processId": {"type": "integer"},
        "parentProcessId": {"type": "integer"},
        "displayName": {"type": "string"},
        "processType": {"type": "string"},
        "pipelineName": {"type": "string"},
        "isActive": {"type": "string"},
        "configurationPayload": {
            "type":"object",
            "properties": {
                "processPayload": {"$ref": "process/process.schema.json#"},
                "dataPayload":{"$ref": "data/data.schema.json#"}
            },
            "required": ["processPayload", "dataPayload"]
        }
    },
    "required": [
        "processId", 
        "parentProcessId",
        "displayName",
        "processType",
        "pipelineName",
        "isActive",
        "configurationPayload"
    ]
}

我该如何解决这个问题,我是 Json 的新手?目前我正在使用在线 Json Scehma 验证器对其进行验证。我还想知道如何使用 PowerShell 脚本针对模式验证 Json 数据?

【问题讨论】:

    标签: json powershell


    【解决方案1】:

    根据提供的架构测试对象

    $schema = @'
    {
      "definitions": {},
      "$schema": "http://json-schema.org/draft-07/schema#",
      "$id": "http://example.com/root.json",
      "type": "object",
      "title": "The Root Schema",
      "required": [
        "name",
        "age"
      ],
      "properties": {
        "name": {
          "$id": "#/properties/name",
          "type": "string",
          "title": "The Name Schema",
          "default": "",
          "examples": [
            "Ashley"
          ],
          "pattern": "^(.*)$"
        },
        "age": {
          "$id": "#/properties/age",
          "type": "integer",
          "title": "The Age Schema",
          "default": 0,
          "examples": [
            25
          ]
        }
      }
    }
    '@
    "{'name': 'Ashley', 'age': '25'}" | Test-Json -Schema $schema
    
    Test-Json : IntegerExpected: #/age
    
    At line:1 char:37
    + "{'name': 'Ashley', 'age': '25'}" | Test-Json -Schema $schema
    +                                     ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Test-Json], Exception
    + FullyQualifiedErrorId : InvalidJsonAgainstSchema,Microsoft.PowerShell.Commands.TestJsonCommand
    False
    
    • 在这个例子中,我们得到一个错误,因为架构需要一个 年龄整数,但我们测试的 JSON 输入使用字符串值 而是。

    • 请参阅此网址中的示例 2 Test-Json

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-24
      • 2020-12-01
      • 1970-01-01
      • 2015-10-24
      • 2017-04-03
      • 2021-11-04
      • 1970-01-01
      相关资源
      最近更新 更多