如何基于json模式中的根对象的值来验证对象? [英] How to validate a object based on the value of root object in json schema?

查看:84
本文介绍了如何基于json模式中的根对象的值来验证对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于订单类型的值来验证销售日期和客户可用性.(请注意,它们不在同一对象下.是否有任何方法可以根据根值来验证子值?

I want to validate sale date and customer availability based on the value of ordertype.(Note., They are not under same object).Is there any way to validate child values based on root value?

 {
      "type": "object",
      "properties": {
        "Order": {
          "type": "object",
          "properties": {
            "OrderDetails": {
              "type": "object",
              "properties": {
                "OrderType": {
                  "type": "string",
                  "enum": [
                    "Y",
                    "N"
                  ]
                }
              }
            }
          }
        },
        "Sale": {
          "type": "object",
          "properties": {
            "Saledate": {
              "format": "date"/*should be present when OrderType is Y*/
            }
          }
        },
        "Customer": {
          "type": "object",
          "properties": {
            "Avilability": {
              "type": "string",
              "enum": [
                "Y",
                "N"
              ]/*should be present when orderType is Y*/
            }
          }
        }
      }
    }

推荐答案

您可以使用if/then创建描述约束.诀窍是您需要在架构中以足够高的级别添加约束,以使其涵盖所有涉及的属性.

You can use if/then to create describe the constraint. The trick is that you need to add the constraint at a high enough level in the schema that it covers all of the properties involved.

{
  ...
  "if": {
    "type": "object",
    "properties": {
      "Order": {
        "type": "object",
        "properties": {
          "OrderDetails": {
            "type": "object",
            "properties": {
              "OrderType": { "const": "Y" }
            },
            "required": ["OrderType"]
          }
        },
        "required": ["OrderDetails"]
      }
    },
    "required": ["Order"]
  },
  "then": {
    "properties": {
      "Sale": { "required": ["Saledate"] },
      "Customer": { "required": ["Availability"] }
    },
    "required": ["Sale", "Customer"]
  }
}

这篇关于如何基于json模式中的根对象的值来验证对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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