use*_*231 4 json jsonschema json-schema-validator
我的 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"
}
}
}
Run Code Online (Sandbox Code Playgroud)
数据总是被#/definitions/userPassword覆盖
我使用此模式得到以下输出
{
"standaloneDeveloper": {
"password": "ABCDEFGHIJKLMNOPQRSTUVWXYZABC",
"confirmPassword": "ABCDEFGHIJKLMNOPQRSTUVWXYZABC"
}
}
Run Code Online (Sandbox Code Playgroud)
预期产出
{
"standaloneDeveloper": {
"firstName": "ABCDEFGHIJKLMNOPQRSTUVWXYZABC",
"lastName": "ABCDEFGHIJKLMNOPQRSTUVWXYZABC",
"emailAddress": "ABCDEFGHI",
"password": "ABCDEFGHIJKLMNOPQRSTUVWXYZABC",
"confirmPassword": "ABCDEFGHIJKLMNOPQRSTUVWXYZABC"
}
}
Run Code Online (Sandbox Code Playgroud)
如何组合 userInfo 和 userPassword?
在 JSON(以及 JSON 模式)中,不能有重复的属性名称。您可以使用allOf它来解决这个问题。
"properties": {
"standaloneDeveloper": {
"allOf": [
{ "$ref": "#/definitions/userInfo" },
{ "$ref": "#/definitions/userPassword" }
]
}
}
Run Code Online (Sandbox Code Playgroud)
这样每个对象$ref中就只有一个。