求助:Azure Logic Apps中跨域依赖属性的JSON Schema验证方案
符合要求的JSON Schema实现
以下是满足验证规则的JSON Schema,适配Azure Logic Apps的验证场景:
{ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "customFields": { "type": "object", "properties": { "paymentMethods": { "type": "array", "items": { "type": "object", "properties": { "tokenizedCard": { "type": "string" }, "cardNumber": { "type": "string" } }, "required": ["tokenizedCard"] } } }, "required": ["paymentMethods"] }, "paymentDetails": { "type": "object", "properties": { "method": { "type": "array", "items": { "type": "object", "properties": { "paymentType": { "type": "string", "enum": ["CREDIT_CARD", "UPI"] }, "tokenizedCard": { "type": "string" } }, "required": ["paymentType", "tokenizedCard"] } } }, "required": ["method"] } }, "required": ["customFields", "paymentDetails"], "allOf": [ { "if": { "properties": { "paymentDetails": { "properties": { "method": { "contains": { "properties": { "paymentType": { "const": "CREDIT_CARD" } } } } } } } }, "then": { "properties": { "customFields": { "properties": { "paymentMethods": { "allOf": [ { "items": { "if": { "properties": { "tokenizedCard": { "enum": { "$data": "/paymentDetails/method[?(@.paymentType == 'CREDIT_CARD')].tokenizedCard" } } } }, "then": { "required": ["cardNumber"] } } }, { "contains": { "properties": { "tokenizedCard": { "enum": { "$data": "/paymentDetails/method[?(@.paymentType == 'CREDIT_CARD')].tokenizedCard" } } } } } ] } } } } } } ] }
关键规则说明
- 基础结构验证:定义JSON的核心结构,确保
customFields.paymentMethods和paymentDetails.method为必填数组,且数组项的基础字段(如tokenizedCard)必填。 - 条件触发逻辑:通过
if判断paymentDetails.method中是否存在paymentType为CREDIT_CARD的项,触发后续验证规则。 - 匹配与必填验证:
- 使用
$data引用提取paymentDetails.method中所有paymentType为CREDIT_CARD的tokenizedCard值。 - 对
customFields.paymentMethods中的每一项,若其tokenizedCard属于上述提取的集合,则强制要求cardNumber为必填项。 - 额外验证
customFields.paymentMethods中必须包含匹配的tokenizedCard项,避免CREDIT_CARD的token在paymentMethods中不存在的情况。
- 使用
注意事项
若Azure Logic Apps当前环境不支持$data关键字,可改用以下替代方案(仅当token值可提前枚举时适用):
{ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "customFields": { "type": "object", "properties": { "paymentMethods": { "type": "array", "items": { "type": "object", "properties": { "tokenizedCard": { "type": "string" }, "cardNumber": { "type": "string" } }, "required": ["tokenizedCard"] } } }, "required": ["paymentMethods"] }, "paymentDetails": { "type": "object", "properties": { "method": { "type": "array", "items": { "type": "object", "properties": { "paymentType": { "type": "string", "enum": ["CREDIT_CARD", "UPI"] }, "tokenizedCard": { "type": "string" } }, "required": ["paymentType", "tokenizedCard"] } } }, "required": ["method"] } }, "required": ["customFields", "paymentDetails"], "allOf": [ { "if": { "properties": { "paymentDetails": { "properties": { "method": { "contains": { "properties": { "paymentType": { "const": "CREDIT_CARD" }, "tokenizedCard": { "const": "11223" } } } } } } } }, "then": { "properties": { "customFields": { "properties": { "paymentMethods": { "contains": { "properties": { "tokenizedCard": { "const": "11223" }, "cardNumber": { "type": "string" } }, "required": ["cardNumber"] } } } } } } } ] }
内容的提问来源于stack exchange,提问作者Prakash H
相关产品推荐
相关产品推荐

