Json模式验证:Json数组中的唯一键

时间:2021-08-10 04:17:55

I have following JSON data:

我有以下JSON数据:

[
  {
  "unique1":{
    "id":3
    }
  },
  {
  "unique2":{
    "id":4
    }
  }
]

Each array item has a json object with one top level unique key. When i try to write a validation schema for it I can only validate that the full array is unique but not the top level key in each array.

每个数组项都有一个具有*唯一键的json对象。当我尝试为它编写一个验证模式时,我只能验证完整的数组是唯一的,而不是每个数组中的*键。

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "type": "array",
  "uniqueItems": true,
  "items": {
    "type": "object",
    "patternProperties": {
      "^.*$": {
      }
    }
  }
}

Following JSON data should fail to validate:

以下JSON数据无法验证:

[
  {
  "unique1":{
    "id":3
    }
  },
  {
  "unique1":{
    "id":4
    }
  }
]

1 个解决方案

#1


1  

There is no standard JSON Schema keyword that allows to express this validation requirement.

没有标准的JSON模式关键字允许表达这种验证需求。

Ajv (for JavaScript) has a custom keyword "uniqueItemProperties" (in ajv-keywords package) that does what you ask for.

Ajv(用于JavaScript)有一个定制的关键字“uniqueItemProperties”(在Ajv -keywords包中),它执行您所要求的操作。

You may propose it for the next versions of the standard.

您可以为下一个版本的标准提出它。

#1


1  

There is no standard JSON Schema keyword that allows to express this validation requirement.

没有标准的JSON模式关键字允许表达这种验证需求。

Ajv (for JavaScript) has a custom keyword "uniqueItemProperties" (in ajv-keywords package) that does what you ask for.

Ajv(用于JavaScript)有一个定制的关键字“uniqueItemProperties”(在Ajv -keywords包中),它执行您所要求的操作。

You may propose it for the next versions of the standard.

您可以为下一个版本的标准提出它。