【问题标题】:JSONSchema to validate an array of strings containing specific itemsJSONSchema 验证包含特定项目的字符串数组
【发布时间】:2020-02-28 11:57:08
【问题描述】:

我正在尝试指定一个必须包含特定属性的字符串数组。

我想指定验证所需的项目。
在此示例中:uuid、模板、createdOn、updatedOn。它必须是一个字符串数组。

我的架构如下所示:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "56544e3c-e197-4602-8457-2c01dc6b12c5",
  "title": "The Root Schema",
  "type": "object",
  "additionalProperties": false,
  "required": ["attributes"],
  "properties": {
    "attributes": {
      "type": "array",
      "uniqueItems": true,
      "items": {
        "type": "string",
        "enum": ["uuid", "template", "createdOn", "updatedOn", "fields", "elements"]
      },
    }
  }
}

并且应该对此进行验证:

{
  "attributes": ["uuid", "template", "createdOn", "updatedOn"],
},

{
  "attributes": ["uuid", "template", "createdOn", "updatedOn", "fields", "elements"],
}

但这不是因为缺少项目“updatedOn”:

{
  "attributes": ["uuid", "template", "createdOn", "fields", "elements"],
}

我已经尝试了很多都没有成功。谁能给个提示?

【问题讨论】:

标签: json jsonschema json-schema-validator


【解决方案1】:

您可以使用contains 关键字来确保数组包含某个项目。

"allOf": [
  { "contains": { "const": "uuid" } },
  { "contains": { "const": "template" } },
  ...
]

【讨论】:

  • 谢谢,我一回到办公室就试试你的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
相关资源
最近更新 更多