我有一个 JSON 对象,其中一个字段包含 JSON 字符串。我需要编写一个 JSON 架构来验证此 JSON 字符串字段。这是我正在使用的 JSON 对象的示例:
{
"id": 1,
"name": "John Doe",
"details": "{\"age\": 30, \"email\": \"[email protected]\"}"
}
“details”是一个具有 JSON 对象的字符串。我想验证细节以具有特定的结构。
我目前拥有的 JSON 模式:
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"details": {
"type": "string",
<Schema to add validation>
如何在 JSON 模式本身中处理这个问题?
这可以使用关键字
contentSchema
来完成,但该关键字只是一个 注释,不需要用于规范的验证。实现可能会强制执行定义的模式。
{
"properties": {
"details": {
"type": "string",
"contentMediaType": "application/json",
"contentSchema": {
"type": "object",
"properties": {
"age": { "type": "integer" },
"email": { "type": "string" }
}
}
}
}
}