使用 JSON 架构 If-Then-Else 更改 属性 类型

Using JSON Schema If-Then-Else to change property type

我最近发现 JSON Schema 07 中可用的 if-then-else 关键字。

我想使用这些关键字根据另一个 属性 的答案更改 属性 的类型。

基本上我想做的是:

"if": {
                "properties" : {
                  "QuestionA": {"enum": ["CorrectAnswer"]}
                }
              },
              "then": {
                "QuestionB": //Change type of question B from null to string 
              }
}

这样做的全部意义在于,当一个问题取决于前一个问题的正确答案时,那么只有第一个问题被正确提出才能回答该问题。

如果我先将第二个问题的类型设置为null,则无法给出答案。第一个问题回答完后,第二个问题的类型变为字符串,可以回答第二个问题。

这在 JSON 架构中是否可以使用 if then else 实现?

是的。

then 关键字值必须是 JSON 架构本身。

如果 if 模式验证成功,将应用 then 模式。

When "if" is present, and the instance successfully validates against its subschema, then valiation succeeds against this keyword if the instance also successfully validates against this keyword's subschema.

https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.6.2