用一个特定的值覆盖一个 "继承的 "JSON属性。

问题描述 投票:0回答:1

考虑下面的示例模式。

"Foo": {
      "type": "object",
      "properties": {
        "num": {
          "type": "integer",
          "minimum": 1,
          "maximum": 64
        }
  }

"Bla": {
      "type": "object",
      "properties": {
        "base": {
          "type": "object",
          "allOf" : [
            {"$ref": "#/definitions/Foo"},
            {"num" : {"enum" : [64]} }
          ]
        }
      }

我想实现的是限制继承属性 "num "的值只能是64,而不是1和64之间的任何值。例如,我想让这个验证 。

"Bla" : {
"base" : {"num" : 64}
}

但不是这个

"Bla" : {
"base" : {"num" : 32}
}
json jsonschema json-schema-validator
1个回答
0
投票

为了完整起见,提供我之前的评论作为答案。

  1. 如果你包住 ”num” 里面 allOf 在另 properties 你应该已经达到了你想要的目的。
  2. 此外,您可能想使用 "const": 64 而不是 "enum": [64] - 以提高其可读性。
  3. 如果你免费使用最新的2019-09号草案,你甚至可以避开。allOf 自从 $ref 然后允许伴有其他关键词,结果是这样的。
{
  "$schema": "https://json-schema.org/draft/2019-09/schema",

...

  "$defs": {
    "Foo": {
      "type": "object",
      "properties": {
        "num": {
          "type": "integer",
          "minimum": 1,
          "maximum": 64
        }
      }
    },
    "Bla": {
      "type": "object",
      "properties": {
        "base": {
          "$ref": "#/$defs/Foo",
          "type": "object",
          "properties": {
            "num": {
              "const" : 64
            }
          }
        }
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.