2016-05-30 5 views
0

私は次のようなスキーマを使用しています。
をご参照のうえ、アレイフィールドに関連アイテムを追加できます。関連アイテムアレイフィールド。私は重複を取得しないように -カスタムオブジェクトを検証する配列にプッシュ

私は同様のオブジェクトが既に配列に存在するかどうかをテストする項目の関連アイテムへフィールドを、押しだオブジェクトをカスタム検証したいと思います。

以下のコードでは、カスタム検証は実行されません。カスタムバリデーションはtype: [object]に適用できないため、オブジェクトのプロパティに適用する必要がありますが、オブジェクト全体をテストすることはできません。

const ItemsSchema = new SimpleSchema({ 
    name: { 
    type: String, 
    label: 'Name', 
    }, 
    related: { 
    type: [Object], 
    label: 'Related Items', 
    optional:true, 
    custom: function() { 
     let queryData = { docId: this.docId, related: this.value } 
     if (Meteor.isClient && this.isSet) { 
     Meteor.call("relatedObjectIsUniqueForThisItem", queryData, 
     function (error, result) { 
      if(!result){ 
      console.log("not unique"); 
      return "Invalid"; 
      } 
      else{ 
      return true; 
      } 
     }); 
     } 
    } 

    }, 
    'related.$.name':{ 
    type: String, 
    label:'Name', 
    }, 
    'related.$.code':{ 
    type:String, 
    label:'Code', 
    min:5, 
    }, 
}); 

答えて

0

私はこれを処理する方法を考え出しました。

カスタム検証は[オブジェクト]ではなく、オブジェクトのプロパティの1つ(この場合は 'source'または 'code')です。

オブジェクトプロパティの1つに、this.siblingField(otherField);を呼び出すことができますが、オブジェクトを再構築する必要があることを意味します。私の場合は

: -

const ItemsSchema = new SimpleSchema({ 
    name: { 
    type: String, 
    label: 'Name', 
    }, 
    related: { 
    type: [Object], 
    label: 'Related Items', 
    optional:true, 
    }, 
    'related.$.name':{ 
    type: String, 
    label:'Name', 
    custom: function() { 

    //--------------------------- 
    //this is the important bit 
    //--------------------------- 
     let queryData = { 
      docId: this.docId, 
      related: { 
       name:this.value, 
       code:this.siblingField('code').value, 
      } 
     } 

     //--------------------------- 
    //end of important bit 
    //--------------------------- 

     if (Meteor.isClient && this.isSet) { 
     Meteor.call("relatedObjectIsUniqueForThisItem", queryData, 
     function (error, result) { 
      if(!result){ 
      console.log("not unique"); 
      return "Invalid"; 
      } 
      else{ 
      return true; 
      } 
     }); 
     } 
    } 

    }, 
    'related.$.code':{ 
    type:String, 
    label:'Code', 
    min:5, 
    }, 
}); 
関連する問題