为什么 `minLengh` 没有验证 JSON 模式中缺失的 属性?

Why is `minLengh` not validating a missing property in JSON Schema?

我正在尝试验证我的对象的 json 模式。 但是 If else 没有为我的对象执行。 所以在这个例子中,我想检查当销售服务 'Y' 时是否有销售描述 (注意,我想执行 if else for object inside object)因为 saleScheduling 在 Sale 对象中。

  "Sale":{
   "type":"object",
   "properties":{
      "SaleScheduling":{
         "type":"object",
         "properties":{
            "SaleDescription":{
               "type":"string"
            },
            "SaleService":{
               "type":"string",
               "minLength":1,
               "enum":[
                  "Y",
                  "N"
               ]
            }
         },
         "if":{
            "properties":{
               "SaleService":{
                  "const":"Y"  //if this is Y, SaleDescription should be present
               }
            }
         },
         "then":{
            "properties":{
               "SaleDescription":{
                  "minLength":1
               }
            }
         },
         "else":{
            "properties":{
               "SaleDescription":{
                  
               }
            }
         },
         "required":[
            "SaleService"
         ]
      }
   },
   "required":[
      "SaleScheduling"
   ]
}

为了使 属性 需要使用 JSON 架构,您需要使用 required 关键字。您在 else...

中的子模式
{
  "properties":{
    "SaleDescription":{
      "minLength":1
    }
}

如果 属性 SalesDescription 存在,这将仅适用 minLength。如果不存在,则不会导致错误。这就是适用性在 JSON 架构上的工作原理。 properties 基于键将子模式应用于对象。 { "minLength": 1 } 本身就是一个子模式,应用于实例中的某个位置。

这是一个 X/Y 问题的例子。您的 if/then 工作正常,但 then 子模式未达到您的预期。