相关疑难解决方法(0)

我如何需要一个或另一个领域或(另外两个)中的一个但不是全部?

我在设置JSON模式时遇到问题,该模式将验证JSON是否包含:

  • 只有一个领域
  • 另一个领域
  • (仅限于其他两个领域之一)

但是当它们的倍数存在时不匹配.

在我的具体情况下,我想要一个

  • copyAll
  • fileNames
  • matchesFiles 和/或 doesntMatchFiles

验证,但我不想接受超过那个.

这是我到目前为止所得到的:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "required": [ "unrelatedA" ],
    "properties": {
    "unrelatedA": {
        "type": "string"
    },
    "fileNames": {
        "type": "array"
    },
    "copyAll": {
        "type": "boolean"
    },
    "matchesFiles": {
        "type": "array"
    },
    "doesntMatchFiles": {
        "type": "array"
        }
    },
    "oneOf": [
         {"required": ["copyAll"], "not":{"required":["matchesFiles"]}, "not":{"required":["doesntMatchFiles"]}, "not":{"required":["fileNames"]}},
         {"required": ["fileNames"], "not":{"required":["matchesFiles"]}, "not":{"required":["doesntMatchFiles"]}, "not":{"required":["copyAll"]}},
         {"anyOf": [
               {"required": ["matchesFiles"], "not":{"required":["copyAll"]}, "not":{"required":["fileNames"]}},
               {"required": ["doesntMatchFiles"], "not":{"required":["copyAll"]}, "not":{"required":["fileNames"]}}]}
    ]
} ;
Run Code Online (Sandbox Code Playgroud)

这比我想要的更多.我希望这匹配以下所有内容:

{"copyAll": true, "unrelatedA":"xxx"}
{"fileNames": …
Run Code Online (Sandbox Code Playgroud)

validation json jsonschema

44
推荐指数
1
解决办法
3万
查看次数

如何根据 JSON Schema 中其他属性的存在有条件地禁止属性?

在我的模式中,我声明了这些属性:

"index_name": {
      "type": "string",
      "examples": ["foo-wwen-live", "foo"]
    },
"locale": {
      "type": "string",
      "examples": ["wwen", "usen", "frfr"]
},
"environment": {
      "type": "string",
      "default": "live",
      "examples": [
        "staging",
        "edgengram",
        "test"
      ]
}
Run Code Online (Sandbox Code Playgroud)

我希望根据我的模式验证的 JSON 主体仅在以下情况下才有效:

  • index_name 存在,并且两者 localeenvironment 存在;
  • locale和/或enviroment 存在,并且index_name 存在

总之,localeenvironment绝不应该以混合index_name

测试用例和预期结果:

这些应该通过:
案例#1

{
  "locale": "usen"
}
Run Code Online (Sandbox Code Playgroud)

案例#2

{
  "environment": "foo"
}
Run Code Online (Sandbox Code Playgroud)

案例#3

{
  "environment": "foo",
  "locale": "usen"
}
Run Code Online (Sandbox Code Playgroud)

案例#4 …

validation json jsonschema

8
推荐指数
1
解决办法
520
查看次数

JSON模式条件:要求和不要求

我正在尝试实现这种条件:如果存在一个特定的属性,则需要另一个属性;但如果不存在,则不需要另一个。

另外,在JSON模式中,可以不使用依赖项吗?

这是一个示例架构

var schema = {
    "properties": {
        "smaller": {
            "type": "number"
        },
        "larger": { "type": "number" },
        "medium":{'type':'string'},
        "bulky":{'type':'string'}
    },
    require:['smaller','larger'],
    additionalProperties:false
};
Run Code Online (Sandbox Code Playgroud)

如果存在“中等”,则需要“庞大”。否则,不需要“庞大”。

此处的“不需要”是指如果“中”不存在,则必须不存在大体积。

jsonschema

5
推荐指数
2
解决办法
2524
查看次数

JSON 模式要求不存在值

我想对我的架构执行 JSON 验证,该架构有四个属性:

  • group
  • partition
  • select
  • features

如果使用 或grouppartitionfeatures需要。如果使用select,则被features禁止(请注意,这似乎与这个问题中的情况不同- 我不想将其设置为features“不需要”,但如果包含它,则会出现验证错误。

所以我的三个选择是:

  • groupfeatures
  • partitionfeatures
  • select并不是features

我将它们编码为:

"oneOf": [
  { "required": [ "group", "features" ] },
  { "required": [ "partition", "features" ] },
  { "required": [ "select" ] }
]
Run Code Online (Sandbox Code Playgroud)

但我不知道如何适当地强制features排除在最后一个选项之外 - 或者这是否有可能?

validation json jsonschema

5
推荐指数
1
解决办法
6618
查看次数

标签 统计

jsonschema ×4

json ×3

validation ×3