使用其他JSON对象中的值作为条件的if / then / else模式验证

时间:2019-01-08 21:05:24

标签: jsonschema

我正在尝试使用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'
                  ],

   ...

   }
}

我希望这段代码将检查PaymentMethod json对象中MyOtherObject字段的值,然后使用基于通过条件检查的模式(如果它等于Cash ),但我仍然收到模式验证错误,说Should not be additional properties 'BankName',这意味着“然后”块中的模式未用于验证。

0 个答案:

没有答案
相关问题