JSON Schema / AJV数组项必须在另一个数组中

时间:2017-10-12 17:05:10

标签: node.js jsonschema ajv

我正在使用Node的AJV(强制执行JSON Schema)。

我想验证array1 #include <stdio.h> #include <string.h> #include <assert.h> int main(int argc, char *argv[]) { char *str = argv[1]; char *rev; int i, j, k; for (i = 0; str[i] != '\0'; i++); { k = i - 1; } for (j = 0; j <= i - 1; j++) { rev[j] = str[k]; k--; } printf("The reverse string is %s\n", rev); return 0; } 。很容易。

然后我想确保array2 properties.bars中的项目在array1 properties.keep中。

我该怎么做?

我有:

properties.bars

1 个答案:

答案 0 :(得分:2)

您可能希望对第一个数组使用$data pointer。现在它只是proposal。它允许您使用另一个属性的 data 值为关键字分配值。

因此,在这种情况下,第二个数组的items属性将具有一个枚举属性,该属性将使用第一个数组的$ data值。

为了做到这一点,我必须删除&#39; anyOf&#39;在原始模式中,以便第一个数组最终不会引用自身。我还通过$ ref和定义将schemaItems组合到主模式中。

此处a PLNKR正在行动中。

测试代码如下所示:

let schema = {
  definitions: {
    schemaItems: {
      id: 'schemaItems',
      type: 'string',
      pattern: '^[^\\s]+ [^\\s]+$'
    }
  },
  type: 'object',
  properties: {
    bars: {
      type: 'array',
      items: {
        $ref: "#/definitions/schemaItems"
      },
      minItems: 1,
      uniqueItems: true
    },
    keep: {
      type: 'array',
      items: {
        type: 'string',
        enum: {
          "$data": "/bars"
        }
      },
      minItems: 0,
      uniqueItems: true
    },
    protect: {
      default: true,
      type: 'boolean'
    }
  }
};

有效的数据样本将是:

let data = {
  bars: [
    "d d",
    "b b"
  ],
  keep: [
    "d d"
  ],
  protect: true
};