每个属性的高级json-schema多个规则

时间:2017-12-15 17:55:14

标签: json node.js jsonschema

我想知道你是否可以拥有多个属性规则。所有的例子似乎都暗示你不能。

所以我希望有类似的东西:

"properties": {
  "track": {
   "type": "string",
   "pattern": "(exclusive)"
  },
  "track": {
   "type": "string",
   "pattern": "(featuring)"
  }
}

我知道在这个例子中要做的显而易见的事情就是让模式成为"(exclusive)|(featuring)",但我想我想知道哪个规则失败了。同样地,我可能想要更复杂的模式,这可能不会被|解决。

1 个答案:

答案 0 :(得分:3)

You can combine schemas with the allOf, anyOf, and oneOf keywords. Each of these keywords takes an array of schemas and does what it says it does. allOf is valid if all of the schemas are valid. anyOf is valid if any of the schemas are valid. oneOf is valid if exactly one of the schemas are valid.

Here's one approach your problem.

{
  "type": "object",
  "properties": {
    "track": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "(exclusive)"
        },
        {
          "type": "string",
          "pattern": "(featuring)"
        }
      ]
    }
  }
}