如何在 Json 架构中有条件地添加其兄弟 json 元素的 json 元素宽度子元素的必填字段?
How to add Required Field conditionally camparing an json element width child element of it's sibling json element in Json Schema?
我正在尝试使用 JSON-Schema 验证示例 JSON 文件。在此,如果类别 是“Private”,type in confidential 是“Customer ”。否则,不需要密码。如何比较不在该定义内的 if-else-then 定义中的值。
schema.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"user": {
"type": "object",
"required": ["name", "email"],
"properties": {
"name": {
"pattern": "(^[a-zA-Z0-9\s]+\b(s\/o|d\/o|a\/l|a\/p)?\b[a-zA-Z0-9\s]*$)",
"maximum": 255,
"minimum": 1
},
"email": {
"maximum": 100,
"format": "email"
},
"password": {
"pattern": "(^[a-zA-Z0-9\s]+\b(s\/o|d\/o|a\/l|a\/p)?\b[a-zA-Z0-9\s]*$)",
"maximum": 25,
"minimum": 8
}
}
},
"confidential": {
"type": "object",
"required": ["id", "version", "type"],
"properties": {
"id": {
"type": "integer",
"maximum": 55,
"minimum": 4
},
"version": {
"maximum": 100,
"type": "number"
},
"type": {
"enum": ["Admin", "Customer"]
}
}
}
},
"required": ["doc_number", "category"],
"properties": {
"user": {
"$ref": "#/definitions/user"
},
"confidential": {
"$ref": "#/definitions/confidential"
},
"doc_number": {
"type": "integer"
},
"category": {
"enum": ["Private", "Public"]
}
}
}
Sample of JSON code is given below
{
"doc_number":12345,
"category": "Private",
"user":{
"name":"Nikola Tesla",
"email":"tesla@teslaNik.com",
"password":"Rvpwv84nExp#E@c"
},
"confidential":{
"type":"Customer",
"version":2.3,
"id":784562
}
}
}
在架构的顶层,在与“properties”和“required”关键字相同的级别,添加一个 if/then 子句来阐明所有条件:
- 类别属性必须存在(
required
),其值必须是某个值(const
)
- 机密属性必须存在(
required
),其值必须是某个值(const
)
在我的架构的顶层添加了 if/then 子句,与“properties”和“required”关键字处于同一级别。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"user": {
"type": "object",
"required": ["name", "email"],
"properties": {
"name": {
"pattern": "(^[a-zA-Z0-9\s]+\b(s\/o|d\/o|a\/l|a\/p)?\b[a-zA-Z0-9\s]*$)",
"maximum": 255,
"minimum": 1
},
"email": {
"maximum": 100,
"format": "email"
},
"password": {
"pattern": "(^[a-zA-Z0-9\s]+\b(s\/o|d\/o|a\/l|a\/p)?\b[a-zA-Z0-9\s]*$)",
"maximum": 25,
"minimum": 8
}
}
},
"confidential": {
"type": "object",
"required": ["id", "version", "type"],
"properties": {
"id": {
"type": "integer",
"maximum": 55,
"minimum": 4
},
"version": {
"maximum": 100,
"type": "number"
},
"type": {
"enum": ["Admin", "Customer"]
}
}
}
},
"required": ["doc_number", "category"],
"properties": {
"user": {
"$ref": "#/definitions/user"
},
"confidential": {
"$ref": "#/definitions/confidential"
},
"doc_number": {
"type": "integer"
},
"category": {
"enum": ["Private", "Public"]
}
},
"if": {
"properties": {
"category": {
"const": "Private"
},
"confidential": {
"properties": {
"type": {
"const": "Customer"
}
}
}
}
},
"then": {
"properties": {
"user": {
"required": ["password"]
}
}
}
}
我正在尝试使用 JSON-Schema 验证示例 JSON 文件。在此,如果类别 是“Private”,type in confidential 是“Customer ”。否则,不需要密码。如何比较不在该定义内的 if-else-then 定义中的值。
schema.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"user": {
"type": "object",
"required": ["name", "email"],
"properties": {
"name": {
"pattern": "(^[a-zA-Z0-9\s]+\b(s\/o|d\/o|a\/l|a\/p)?\b[a-zA-Z0-9\s]*$)",
"maximum": 255,
"minimum": 1
},
"email": {
"maximum": 100,
"format": "email"
},
"password": {
"pattern": "(^[a-zA-Z0-9\s]+\b(s\/o|d\/o|a\/l|a\/p)?\b[a-zA-Z0-9\s]*$)",
"maximum": 25,
"minimum": 8
}
}
},
"confidential": {
"type": "object",
"required": ["id", "version", "type"],
"properties": {
"id": {
"type": "integer",
"maximum": 55,
"minimum": 4
},
"version": {
"maximum": 100,
"type": "number"
},
"type": {
"enum": ["Admin", "Customer"]
}
}
}
},
"required": ["doc_number", "category"],
"properties": {
"user": {
"$ref": "#/definitions/user"
},
"confidential": {
"$ref": "#/definitions/confidential"
},
"doc_number": {
"type": "integer"
},
"category": {
"enum": ["Private", "Public"]
}
}
}
Sample of JSON code is given below
{
"doc_number":12345,
"category": "Private",
"user":{
"name":"Nikola Tesla",
"email":"tesla@teslaNik.com",
"password":"Rvpwv84nExp#E@c"
},
"confidential":{
"type":"Customer",
"version":2.3,
"id":784562
}
}
}
在架构的顶层,在与“properties”和“required”关键字相同的级别,添加一个 if/then 子句来阐明所有条件:
- 类别属性必须存在(
required
),其值必须是某个值(const
) - 机密属性必须存在(
required
),其值必须是某个值(const
)
在我的架构的顶层添加了 if/then 子句,与“properties”和“required”关键字处于同一级别。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"user": {
"type": "object",
"required": ["name", "email"],
"properties": {
"name": {
"pattern": "(^[a-zA-Z0-9\s]+\b(s\/o|d\/o|a\/l|a\/p)?\b[a-zA-Z0-9\s]*$)",
"maximum": 255,
"minimum": 1
},
"email": {
"maximum": 100,
"format": "email"
},
"password": {
"pattern": "(^[a-zA-Z0-9\s]+\b(s\/o|d\/o|a\/l|a\/p)?\b[a-zA-Z0-9\s]*$)",
"maximum": 25,
"minimum": 8
}
}
},
"confidential": {
"type": "object",
"required": ["id", "version", "type"],
"properties": {
"id": {
"type": "integer",
"maximum": 55,
"minimum": 4
},
"version": {
"maximum": 100,
"type": "number"
},
"type": {
"enum": ["Admin", "Customer"]
}
}
}
},
"required": ["doc_number", "category"],
"properties": {
"user": {
"$ref": "#/definitions/user"
},
"confidential": {
"$ref": "#/definitions/confidential"
},
"doc_number": {
"type": "integer"
},
"category": {
"enum": ["Private", "Public"]
}
},
"if": {
"properties": {
"category": {
"const": "Private"
},
"confidential": {
"properties": {
"type": {
"const": "Customer"
}
}
}
}
},
"then": {
"properties": {
"user": {
"required": ["password"]
}
}
}
}