2016-09-15 17 views
0

ここに私が持っている小さなJSONファイルがあります。 validate_doc_update関数で宛先配列のアドレスフィールドが必要になります。
{ "名前": "誰か"、 "secondary_id": "1111"、 "宛先":[ { "アドレス": "何か" }] }CouchDBで配列内のフィールドを要求する方法はありますか?

私はこれを試みました(もちろん働いていません):

function(newDoc, oldDoc, userCtx){ 
if(!newDoc.destination.address){ 
throw({forbidden:'doc.address is required'}); 
} 
} 

配列にフィールドを必要とする方法はありますか?またはcouchDBは許可していませんか?あなたがaddressを含むようにdestinationアレイ内のすべてのオブジェクトが必要な場合は、

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を含むことができ、あなたは次の検証機能を使用することができると仮定すると

答えて

0

検証機能の後に:

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'}); 
} 
+0

私が探していたのは最初のものでした。それは完全に働いた。私はそのような単純な解決策を考えなかったとは思えません。私は自分自身のためにものをovercomplicatedしていた。どうもありがとうございました。 – Anna

関連する問題