此架构是否达到条件的必需属性?

时间:2019-11-26 22:17:47

标签: json jsonschema

我的架构规则能达到我想要的结果吗?本质上,如果email等于phone,则属性messagesuccess是必需的,否则可能不存在。

{
    "message": "success", // REQUIRED can be "success", "user does not exist"
    "email": "foo@bar.com", // CONDITIONALLY REQUIRED if message="success" otherwise can be null or ""
    "phone": "0210001000", // CONDITIONALLY REQUIRED if message="success" otherwise can be null or ""
}

此架构是否可以满足我的上述需求?

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "The Root Schema",
  "type": "object",
  "if": {
    "properties": {
      "message": { "const": "success" }
    },
    "required": ["email", "phone"]
  },
  "else": { 
    "required": ["message"] 
  },
  "properties": {
    "message": {
      "type": "string"
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "phone": {
      "type": "string"
    }
  }
}

0 个答案:

没有答案