是否有辦法為驗證檔案定義JSON模式,該檔案具有遞回性質。
例如:我的用例是評論中可以有嵌套的評論。
"comments": [
{
"user" : "xxx1",
"message" : "yyyyy1",
"comments" : [
{
"user"/span> : "xxx2"/span>,
"message" : "yyyyy2",
"comments" : [
//等等。
]
}
]
}
不知道如何為像這樣的驗證用例定義一個JSON模式。
uj5u.com熱心網友回復:
你可以通過將遞回位放在定義中,然后用$defs(或者definitions,如果你使用的是draft7或更早)來參考它來定義遞回模式:
{
"$defs"/span>: {
"comments": {
"anyOf": [
{
......當它沒有更多的嵌套時,評論是什么樣子的?"type": "string"也許?
},
{
"type": "陣列"。
"專案": {
"type": "object",
"屬性": {
...你的其他屬性...
"comments": { ...你的其他屬性。
"$ref"。"#/$defs/comments"。
}
}
}
}
]
}
},
... 其余的你的模式,這將使用一個"$ref"。"#/$defs/comments"某處...
}
在https://json-schema.org/understanding-json-schema/structuring.html#recursion也有一個這樣的例子。
uj5u.com熱心網友回復:
如果你使用NodeJS(Express),你可以這樣做,使用Mongoose:
var commentSchema = new Schema();
commentSchema.add( {
user: { type: mongoose.Schema. Types.ObjectId, ref。'USERS', required: true }。
message: { type: String, required: true }。
comments: [ commentSchema ]
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/311813.html
標籤:
上一篇:Tcl中的%P是什么?
