在if / then [json]中向ref添加引用路径

问题描述

请帮助我,我有一个文件,需要验证一些数据。对于具有相同属性的字段,可以使用下面的代码运行。但是我也有不在相同“属性”下的字段。

"if": {
  "properties": {
    "type": { "const": "one" }
  }
   },"then": { "required": ["test"] } }

所以,我正在做的是向所需语句添加引用路径。我尝试过了

"then": { "required": {"$ref" : "#/path/example" }}

但是没有用。有什么想法可以在必需项内使用引用路径吗?如果可以,怎么办?

解决方法

我认为您要表达的是,如果/type"one",则需要/path/example

因此,这些应该有效:

{
  "type": "one","path": { "example": 42 }
}
{
  "type": "two","path": { "foo": 42 }
}

而且,这应该无效:

{
  "type": "one","path": { "foo": 42 }
}

这是我们可以做到的:

"if": {
  "properties": {
    "type": { "const": "one" }
  }
},"then": {
  "properties": {
    "path": { "required": ["example"] }
  },"required": ["path"]
}

您需要使用properties(如果您需要进入两个以上级别,则可能是几个嵌套的properties)来建立路径,并在每个级别使用required

(如果我仍然不明白您的问题,请告诉我,我们可以重试)