我以以下方式将产品存储在 CouchDB 数据库中:
{
"_id": "1ce330a867a803fd10082c4513000fe5",
"_rev": "4-a5eae6f790ea8b9cedea53db50a13fef",
"type": "Product",
"name": "Apple",
"price": 1
}
我想创建一个验证,以确保每个带有 的新文档type
Product
都有该字段name
。我找到的唯一文档是http://guide.couchdb.org/draft/validation.html阅读后我写/复制了这个验证:
function(newDoc, oldDoc, userCtx) {
function require(field, message) {
message = message || "Document must have a " + field;
if (!newDoc[field]) throw({forbidden : message});
}
if (newDoc.type == "Product") {
require("name");
}
}
比我使用 Fauxton 来创建它:
但它不起作用。我可以创建这样一个没有name
字段的文档。我做错了什么?