尝试使用 json 模式验证在 azure 逻辑应用程序中验证 json。
这是我的示例 json:
{
"address": [
{
"contact": {
"firstName": "myFirstName",
"lastName": "myLastName"
},
"type": "bill"
}
]
}
这是我的模式:
{
"if": {
"properties": {
"address": {
"type": "array",
"items": {
"properties": {
"type": {
"const": "bill"
}
}
}
}
}
},
"then": {
"properties": {
"address": {
"type": "array",
"items": {
"properties": {
"contact": {
"type": "object",
"required": [
"firstName"
]
}
}
}
}
}
}
}
如果我没有在输入 json 中传递 firstName 属性,验证将失败,这是预期的,但我收到的错误消息是不正确的。它不显示缺少哪个属性。就像在这个例子中一样,我期待消息应该告诉 firstName 属性丢失。
这是上述验证的输出:
"outputs": {
"errors": [
{
"message": "JSON does not match schema from 'then'.",
"lineNumber": 0,
"linePosition": 0,
"path": "",
"schemaId": "#/then",
"errorType": "then",
"childErrors": []
}
]
}
任何帮助将不胜感激
我已经在我的环境中重现并得到如下预期结果:
首先,我采用了一个 http 触发器,然后采用了解析 json 的步骤,并在其中给出了以下架构。
{
"type": "object",
"properties": {
"address": {
"type": "array",
"items": {
"type": "object",
"properties": {
"contact": {
"type": "object",
"properties": {
"firstName": {
"type": "string",
"message": {
"required": "name is required"
},
"required": true
},
"lastName": {
"type": "string"
}
}
},
"type": {
"type": "string"
}
},
"required": [
"contact",
"type"
]
}
}
}
}
现在响应行动:
outputs('Parse_JSON')['errors'][0]['message']
在我保持 runAfter Failed 获取消息之后,您也可以使用任何并行操作。
输出:
逻辑应用程序中的输出:
代码视图:
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Parse_JSON": {
"inputs": {
"content": "@triggerBody()",
"schema": {
"properties": {
"address": {
"items": {
"properties": {
"contact": {
"properties": {
"firstName": {
"message": {
"required": "name is required"
},
"required": true,
"type": "string"
},
"lastName": {
"type": "string"
}
},
"type": "object"
},
"type": {
"type": "string"
}
},
"required": [
"contact",
"type"
],
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
},
"runAfter": {},
"type": "ParseJson"
},
"Response": {
"inputs": {
"body": "@outputs('Parse_JSON')['errors'][0]['message']",
"statusCode": 200
},
"kind": "Http",
"runAfter": {
"Parse_JSON": [
"Failed"
]
},
"type": "Response"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"manual": {
"inputs": {
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}
你只能这样做,我同意@skin 的观点。