【问题标题】:JSON Schema: Combine multiple referencesJSON Schema:组合多个引用
【发布时间】:2020-10-21 17:29:02
【问题描述】:

我的 Json 架构

{
  "$schema": "http://json-schema.org/draft-07/schema#",
    
  "definitions": {
    
    "userInfo": {
      "type": "object",
      "properties": {
        "firstName": { "type": "string" },
        "lastName":  { "type": "string" },
        "emailAddress":{ "type": "string" }
      },
      "required": ["firstName", "lastName", "emailAddress"]
    },
    
    "userPassword": {
      "type": "object",
      "properties": {
        "password": { "type": "string" },
        "confirmPassword":  { "type": "string" }
      }
    }
  },

  "type": "object",
    
  "properties": {
    "standaloneDeveloper": {
      "$ref": "#/definitions/userInfo",
      "$ref": "#/definitions/userPassword"
    } 
  }
}

数据总是被 #/definitions/userPassword

覆盖

我使用此架构得到以下输出

{
  "standaloneDeveloper": {
    "password": "ABCDEFGHIJKLMNOPQRSTUVWXYZABC",
    "confirmPassword": "ABCDEFGHIJKLMNOPQRSTUVWXYZABC"
  }
}

预期输出

{
  "standaloneDeveloper": {
    "firstName": "ABCDEFGHIJKLMNOPQRSTUVWXYZABC",
    "lastName": "ABCDEFGHIJKLMNOPQRSTUVWXYZABC",
    "emailAddress": "ABCDEFGHI",
    "password": "ABCDEFGHIJKLMNOPQRSTUVWXYZABC",
    "confirmPassword": "ABCDEFGHIJKLMNOPQRSTUVWXYZABC"
  }
}

如何结合 userInfo 和 userPassword?

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    在 JSON(因此也包括 JSON Schema)中,您不能有重复的属性名称。你可以使用allOf 来解决这个问题。

    "properties": {
      "standaloneDeveloper": {
        "allOf": [
          { "$ref": "#/definitions/userInfo" },
          { "$ref": "#/definitions/userPassword" }
        ]
      }
    }
    

    这样每个对象中只有一个$ref

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-22
      • 2021-04-18
      • 2017-08-24
      • 2021-12-19
      • 2020-11-17
      • 1970-01-01
      • 2020-07-11
      • 2011-12-19
      相关资源
      最近更新 更多