Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 当布尔属性a为真时,如何要求属性B_Json_Jsonschema - Fatal编程技术网

Json 当布尔属性a为真时,如何要求属性B

Json 当布尔属性a为真时,如何要求属性B,json,jsonschema,Json,Jsonschema,使用JSON模式,当属性foo(aboolean)设置为true时,如何指定属性bar是必需的 我尝试了以下方法: { "type": "object", "properties": { "foo": { "type": "boolean" }, "bar": { "type": "string" } }, "if": { "properties": { "foo": { "boolean": true } }, "requir

使用JSON模式,当属性
foo
(a
boolean
)设置为
true
时,如何指定属性
bar
是必需的

我尝试了以下方法:

{
  "type": "object",
  "properties": {
    "foo": { "type": "boolean" },
    "bar": { "type": "string" }
  },
  "if": {
    "properties": {
      "foo": { "boolean": true }
    },
    "required": ["foo"]
  },
  "then": { "required": ["bar"] }
}
但是,这无法验证:

{
  "foo": false
}
因为尽管
foo
false
,但似乎仍然需要
bar

Message:
    Required properties are missing from object: bar.
Schema path:
    #/then/required
我也试过:

{
  "type": "object",
  "properties": {
    "foo": { "type": "boolean" },
    "bar": { "type": "string" }
  },
  "if": {
    "properties": {
      "foo": { "const": "true" }
    },
    "required": ["foo"]
  },
  "then": { "required": ["bar"] }
}
但它接受以下JSON,这应被视为无效:

{
  "foo": true
}
我已经看到了,但是它使用类型
string
表示
foo
,这很好。我需要
foo
成为
boolean


foo
true
时,我如何规定需要
bar

您使用的
const
是正确的,但是该值是一个字符串,而不是布尔值。你想要

“foo”:{“const”:true}


我投票决定结束,因为这是一个简单的打字错误。