Javascript 发布JSON时出现ValidationError
我为作者提供了以下模式:Javascript 发布JSON时出现ValidationError,javascript,json,node.js,validationerror,Javascript,Json,Node.js,Validationerror,我为作者提供了以下模式: module.exports = { "id": "Author", "properties": { "name": { "type": "string", "description": "The full name of the author" }, "description": { "type": "string", "description": "A small bio of the
module.exports = {
"id": "Author",
"properties": {
"name": {
"type": "string",
"description": "The full name of the author"
},
"description": {
"type": "string",
"description": "A small bio of the author"
},
"books": {
"type": "array",
"description": "The list of books published on at least one of the stores by this author",
"items": {
"$ref": "Book"
}
},
"website": {
"type": "string",
"description": "The website url of the author"
},
"avatar": {
"type": "string",
"description": "The url of the avatar of this author"
}
}
}
当我发布到以创建新作者时,出现以下错误:
错误:找到InternalServerError类型的错误:ValidationError:books:对路径books处的值['TheTwits','TheBFG']转换到数组失败
这是我发布的JSON
{
"name": "Roald Dahl",
"description": "Writes childrens novels",
"books": [
"The Twits",
"The BFG"
],
"website": "www.roalddahl.com",
"avatar": "https://www.natgeokids.com/wp-content/uploads/2016/11/Roald-Dahl-1-1.jpg"
}
据我所知,这个JSON是正确的。该错误似乎表明books数组存在问题。是这样吗?如果是,我该如何解决
正在添加书本架构:
module.exports = {
"id": "Book",
"properties": {
"title": {
"type": "string",
"descrtiption": "The title of the book"
},
"authors": {
"type": "array",
"description": "List of authors of the book",
"items": {
"$ref": "Author"
}
},
"isbn_code": {
"type": "string",
"description": "The stores where clients can buy this book"
},
"stores": {
"type": "array",
"description": "The stores where clients can buy this book",
"items": {
"type": "object",
"properties": {
"store": {
"$ref": "Store"
},
"copies": {
"type": "integer"
}
}
}
},
"genre": {
"type": "string",
"description": "Genre of the book",
},
"description": {
"type": "string",
"description": "Description of the book"
},
"reviews": {
"type": "array",
"items": {
"$ref": "ClientReview"
}
},
"price": {
"type": "number",
"minimum": 0,
"description": "The price of this book"
}
}
}
您正在将JSON与books字段一起作为字符串数组发布,因此您的validator对象应该指定:
items: { type: "string" },
items:{$ref:Book}是什么意思?我使用在线工具进行测试,如果没有,数据是有效的$ref在JSON模式中非常好;假设一个Book对象有一个模式,那么OP在这里使用的ref表示数组将包含Book对象。但是在JSON文档中,OP使用的是字符串,而不是book对象。这段代码来自使用Node.js进行REST API开发的书籍。我正在学习,所以有一些事情我不确定$参考。记住这一点,我的JSON可能错误地在书中使用了字符串。假设它应该使用book对象,我将如何更改JSON以使用它?我需要先加书吗?