2016-03-30 8 views
0

私のモデルは、私は、これは失敗するとエラーメッセージが表示されます期待し帆ウォーターラインモデルが検証タイプ「整数」を属性、「フロート」は

per: { type: 'float', required: true }, typeid: { type: 'integer', required: true },

マイ入力

{ per: '5GH', typeid: '6SD', }

属性失敗します何かのように

typeid: [ { rule: 'integer', message: 'undefined should be a integer

しかし、検証上のO/Pの検証

{ per: 5, typeid: 6, }

た後、私たちは手動で、この場合には、整数と浮動小数点数を検証する必要がありますか?ドキュメントのよう

答えて

2

Official Sails Doc for Validation

あなたはその整数の検証整数のチェックだけでなく、検証のための文字列を見ることができます。

私は厳密にintegerの代わりに使用intdecimalを検証するための検証

で経験してきたものによると、あなたのシーンにfloat

問題は以下の通りです。

a=5  =>integer in a 
a="5" =>string but the integer value is 5 
a="5a4" =>string but integer value is 5 not 54 

a="a5" =>string and no integer value.Only this case will fail on validation rule 

したい場合は厳密に、あなたはあなたのmodels.See以下のコードでカスタム検証ルールを追加することができますルールカスタム君に応じて属性を検証:各モデルのためにそう

module.exports = { 
    attributes: { 
     name: { 
      type:'string' 
     }, 
     mail: { 
      type:'string', 
      defaultsTo:'a' 
     }, 
     age:{ 
      type:'string', 
      isInt:true 
     } 
    }, 
    types:{ 
     isInt:function(value){ 
      console.log(value); 
      if(typeof value==='number' && value=== Math.floor(value)){ 
       console.log('value is a number'); 
       return true; 
      } 
      if(!isNaN(1*value)){ 
       console.log(value); 
       return true; 
      } 
      return false; 
     } 
    } 
}; 

をカスタムバリデーターを記述する必要があります。私が推測する そして

あなたはグローバル検証 を書き込むことによって、異なるモデルの属性に関するあなたの検証を適用することができるようにグローバルなカスタム検証に ルールを記述するための方法現在、今そこにあります。

enter image description here

enter image description here

+0

ありがとうございます。それは動作します.. @ waza007 – Max

+0

ようこそ、依頼し続ける! – vkstack