jsonschema - 以其他 json 对象中的值为条件使用 if/then/else 模式验证
问题描述
我正在尝试使用 if/then/else 为对象使用三种不同的模式,但基于该对象之外的 JSON 字段的值。我只见过人们根据同一对象中的字段制作条件的例子。
这是我尝试过的:
schema: {
'MyOtherObject': {
'$id': '#/properties/MyOtherObject',
'type': 'object',
'additionalProperties': false,
'title': 'The MyOtherObject Schema',
'required': [
'PaymentMethod'
],
'properties': {
'PaymentMethod': {
'$id': '#/properties/Deal/properties/PaymentMethod',
'type': 'string',
'title': 'The PaymentMethod Schema',
'default': '',
'examples': [
'Cash'
],
'pattern': '^(.*)$'
},
...
},
'MyConditionalSchemaObject': {
'$id': '#/properties/MyConditionalSchemaObject',
'type': 'object',
'additionalProperties': false,
'title': 'The MyConditionalSchemaObject Schema',
'if': {
// Trying to get at the value above but don't know how... my schema validations still failing
'2/MyOtherObject/properties/PaymentMethod' : { 'const': 'Cash' }
},
'then': {
'required': [
'PaymentStartDate'
],
'properties': {
'BankName': {
'$id': '#/properties/Financing/properties/BankName',
'type': ['string', 'null'],
'title': 'The Bankname Schema',
'default': '',
'examples': [
'Capital One Auto Finance'
],
...
}
}
我希望这段代码会检查 json 对象中PaymentMethod
字段的值,MyOtherObject
然后根据通过条件检查(如果它等于Cash
)使用架构,但我仍然收到架构验证错误,说Should not be additional properties 'BankName'
,,暗示架构在“then”块中没有用于验证。
解决方案
推荐阅读
- paypal - 带有额外选项的 PayPal 订阅
- c - 禁用 GtkStackSidebar 小部件上的选项卡功能
- r - R是说已经有色标,而没有?
- angular - TS7016:找不到模块“rxjs”的声明文件
- android - 有没有办法为 Android 编译一个 SwiftUI 应用程序
- r - 如何将几个月内的总积分制表?
- javascript - 使用 D3 在“scrollytelly”Mapbox GL JS 地图上绘制点
- c# - 请求数据未从视图传递到控制器
- python - 如何在 Python 中显示每个单独变量的相关系数?
- django - 如何更改 django 发送到前端的日期时间格式?