JSON模式条件:要求和不要求 [英] JSON Schema conditional: require and not require

查看:66
本文介绍了JSON模式条件:要求和不要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to implement this condition: if a particular property exists, then another property is required; but if it does not exist, another one is not required.

还可以在JSON模式中不使用依赖项吗?

Also, in JSON schemas, can we use not in dependencies?

这是示例架构

var schema = {
    "properties": {
        "smaller": {
            "type": "number"
        },
        "larger": { "type": "number" },
        "medium":{'type':'string'},
        "bulky":{'type':'string'}
    },
    require:['smaller','larger'],
    additionalProperties:false
};

如果存在中",则需要大".否则,不需要庞大".

If "medium" is present, then "bulky" is required. Otherwise, "bulky" is not required.

这里不需要"表示如果中"不存在,那么就不能有大块.

Here "not required" means that if "medium" doesn't exist, then bulky must not be present.

推荐答案

即使不使用JSON Schema draft-07 if-then-else ,也有几种方法可以达到所需的效果.

There are several ways to achieve required effect even not using JSON Schema draft-07 if-then-else.

此处的逻辑含义是:如果存在中等",则要求大块" 可以翻译为中等",否则必需" (后者表示存在"medium" ),可以进一步详细说明为"medium"不是必需的,或者是"bulky"为"required" (因为如果"medium"为目前,它将满足所需条件).参见以下架构:

A logical implication here: if "medium" present then "bulky" is required can be translated to "medium" not present OR "bulky" is "required" (the latter implicates "medium" is present) which can be further elaborated to "medium" not required OR "bulky" is "required" (since if "medium" is present, it will satisfy condition of being required). See below schema:

"properties": {
  "smaller": {"type": "number"},
  "larger": { "type": "number" },
  "medium":{"type":"string"},
  "bulky":{"type":"string"}
},
"required":["smaller","larger"],
"anyOf" : [ 
  { 
    "not" : { "required" : ["medium"] }
  },
  {
    "required" : ["bulky"]
  }
],
"additionalProperties" : false

在此处检查以供参考:

JSON模式-如果对象确实有效*不*包含特定属性

http://json-schema.org/Latest/json-schema-validation.html#rfc.section.6.7

"anyOf"-逻辑或,"oneOf"-XOR,"allOf"-AND,"not"-取反,但请注意规范:

"anyOf" - logical OR, "oneOf" - XOR, "allOf" - AND, "not" - negation, yet pay attention to spec:

如果实例无法针对此关键字定义的架构成功验证,则该实例对该关键字有效.

An instance is valid against this keyword if it fails to validate successfully against the schema defined by this keyword.

草稿-06-依赖项+ propertyNames

最明显.我不确定您是否在问题中排除了这一点,因此以防万一.请注意,如果您不想简单地限制有效密钥,则可以使用"propertyNames"(实际上是为其添加的内容)来代替"additionalProperties".

draft-06 - dependencies + propertyNames

Most obvious. I am not sure if you excluded this one in your question, so putting here just in case. Please note, that instead of "additionalProperties", if you wan't simply to limit valid keys, "propertyNames" could be used (and is actually what it was added for).

"properties": {
  "smaller": {"type": "number"},
  "larger": { "type": "number" },
  "medium":{"type":"string"},
  "bulky":{"type":"string"}
},
"required":["smaller","larger"],
"dependencies" : {
  "medium" : ["bulky"]
},
"propertyNames" : {
  "enum" : [
    "smaller",
    "larger",
    "medium",
    "bulky"
  ]
}

在此处检查以供参考: http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.5.7

Check here for reference: http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.5.7

在评论中澄清后:

第6草案-这里的不需要"表示如果中等"不存在,那么笨重的一定不能存在"

for draft-6 - here "not require" means that if "medium" dont exist then bulky " must not be present"

不得"表示防止体积大.

"must not" means preventing bulky being present.

我将重述您的病情:

1.如果中"存在,则大"必须存在->两个密钥必须同时存在

2.如果中"不存在,则大" 也不能存在->两个键一定不能同时存在

2. if "medium" does not exist "bulky" must not be present as well -> both keys must not be present at the same time

大块"是否存在,中"不存在?

Can "bulky" exist and "medium" does not exist?

不.请参阅2.反之亦然(请参阅1.).布尔等式(与逻辑XOR互补).

No. See 2. And vice versa (see 1.). Boolean equality (complementary to logical XOR).

因此,如果大量"存在-则意味着中"必须始终存在...这意味着两者都是必需的,或者都不需要(甚至不允许) .

Thus if "bulky" exists - it means "medium" must be always there... It implies that both are required or both must not be required (or even allowed).

因为它是草案06,所以您还可以使用"propertyNames" 来定义允许的属性名称(这种逻辑的捷径).

Since it's draft-06, you can use also "propertyNames" for defining allowed property names (kind of shortcut to this logic).

翻译为JSOn Schema的正确逻辑操作如下:

The proper logical operation translated to JSOn Schema would look like:

"oneOf" : [
  { "required" : ["medium","bulky"] }, <== this schema is satisfied if both keys appear in validated instance
  {
    "allOf" : [   <== !medium ^ !bulky - due to how "not" works in schema context
      {"not" : { "required" : ["medium"] } },  
      {"not" : { "required" : ["bulky"] } },
    ]
  }
]

异或(全部都需要)或(不需要中等而不需要笨重).

An XOR - EITHER (both required) OR (medium not required AND bulky not required).

请注意,我没有执行"not":{"required":["medium","bulky"]} ,因为只有这些键之一存在时,"required"模式才会失败,表示不"将返回成功的验证结果.需要使用de Morgans法来重新定义它:

Please note I am not doing "not" : { "required" : ["medium","bulky"] } as when just one of those keys is present, "required" schema would fail which would mean "not" would return successfull validation result. One needs to rephrase it using de Morgans laws:

"oneOf" : [
  { "required" : ["medium","bulky"] },
  {
    "not" : {   <=== !medium ^ !bulky = !(medium v bulky)
      "anyOf" : [
        { "required" : ["medium"] },
        { "required" : ["bulky"]  },
      ]
    }
  }
]

但是,使用"propertyNames"也可以解决问题. 请参阅以下架构:

However using "propertyNames" will also do the trick. See following schema:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "properties": {
    "smaller": {"type": "number"},
    "larger": { "type": "number" },
    "medium":{"type":"string"},
    "bulky":{"type":"string"}
  },
  "required":["smaller","larger"],
  "anyOf" : [ 
    { 
       "required" : ["medium","bulky"]
    },
    {
      "propertyNames" : {
        "enum" : [
          "smaller",
          "larger"
        ]
      },
    }
  ],
  "examples" : [
    {
      "smaller" : 1,
      "larger" : 2,


    },
    {
      "smaller" : 1,
      "larger" : 2,
      "bulky" : "test",
      "medium" : ""
    },
    {
      "smaller" : 1,
      "larger" : 2,

      "medium" : ""
    },
    {
      "smaller" : 1,
      "larger" : 2,
      "bulky" : "test",

    },
  ]
}

它能回答您的问题吗?

这篇关于JSON模式条件:要求和不要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆