您如何在 CouchDB 的数组中需要一个字段?
How can you require a field in an array in CouchDB?
这是我的 JSON 小文件。我希望 validate_doc_update 函数需要目标数组中的地址字段。
{
"Name": "Someone",
"secondary_id": "1111",
"destination": [
{
"address": "something"
}]
}
这个我试过了(当然不行):
function(newDoc, oldDoc, userCtx){
if(!newDoc.destination.address){
throw({forbidden:'doc.address is required'});
}
}
有没有办法在数组中要求一个字段?还是 couchDB 不允许?
假设destination
数组中任意一个对象都可以包含address
才能验证成功,可以使用如下验证函数:
function(newDoc, oldDoc, userCtx) {
if(newDoc.destination && newDoc.destination.length > 0){
for(var i = 0; i < newDoc.destination.length; i++){
if(newDoc.destination[i].address) return;
}
}
throw({forbidden:'doc.address is required'});
}
如果要求destination
数组中的所有对象都包含address
,请使用以下验证函数:
function(newDoc, oldDoc, userCtx) {
if(newDoc.destination && newDoc.destination.length > 0){
for(var i = 0; i < newDoc.destination.length; i++){
if(!newDoc.destination[i].address) throw({forbidden:'doc.address is required'});
}
return;
}
throw({forbidden:'doc.address is required'});
}
这是我的 JSON 小文件。我希望 validate_doc_update 函数需要目标数组中的地址字段。
{
"Name": "Someone",
"secondary_id": "1111",
"destination": [
{
"address": "something"
}]
}
这个我试过了(当然不行):
function(newDoc, oldDoc, userCtx){
if(!newDoc.destination.address){
throw({forbidden:'doc.address is required'});
}
}
有没有办法在数组中要求一个字段?还是 couchDB 不允许?
假设destination
数组中任意一个对象都可以包含address
才能验证成功,可以使用如下验证函数:
function(newDoc, oldDoc, userCtx) {
if(newDoc.destination && newDoc.destination.length > 0){
for(var i = 0; i < newDoc.destination.length; i++){
if(newDoc.destination[i].address) return;
}
}
throw({forbidden:'doc.address is required'});
}
如果要求destination
数组中的所有对象都包含address
,请使用以下验证函数:
function(newDoc, oldDoc, userCtx) {
if(newDoc.destination && newDoc.destination.length > 0){
for(var i = 0; i < newDoc.destination.length; i++){
if(!newDoc.destination[i].address) throw({forbidden:'doc.address is required'});
}
return;
}
throw({forbidden:'doc.address is required'});
}