JSON模式-根据其他值有条件地将$ ref应用于值?

时间:2019-07-28 01:59:55

标签: jsonschema

我想基于架构中的另一个值有条件地针对$ ref进行验证。

items: {
    type: 'object',
    properties: {
      kind: { type: 'string', enum: ['foo', 'bar'] },
      //parameters: { $ref: 'foo.json#' } // This works
      parameters: {
        if: {
          kind: 'foo'
        },
        then: {
          $ref: 'foo.json#'
        }
      }
      // also tried   
      if: {
        kind: 'foo'
      },
      then: {
        parameters: { $ref: 'foo.json#' }
      }

只要parameters的值等于foo.json(与kind和{{相同),我希望根据foo引用对bar进行验证。 1}})。但是上述方法不起作用。取消注释部分的注释是可行的,因此它们不是等效的。

如何格式化该格式,以便有条件地将$ ref应用于基于其他值的值?

对于bar.json枚举,我实际上有大约10个不同的值,因此,如果有一种更干净的方法,比[if / else]我更开放。

1 个答案:

答案 0 :(得分:0)

啊明白了......

items: {
  type: 'object',
  properties: {
    kind: { type: 'string', enum: ['foo', 'bar'] }
  },
  required: ['parameters'],
  if: {
    properties: { kind: { const: 'foo' } },
    required: ['kind']
  },
  then: {
    properties: {
      parameters: {
        $ref: 'fpp.json#'
      }
    }
  }

认为ifelse中的任何内容基本上都与主架构合并对我很有帮助。