2014-09-13 27 views

回答

4

只需使用String,並添加一些驗證,以確保輸入的是正確的類型:

COLORS = ['red', 'blue', ...]; 

function colorValidator (v) { 
    if (v.indexOf('#') == 0) { 
     if (v.length == 7) { // #f0f0f0 
      return true; 
     } else if (v.length == 4) { // #fff 
      return true; 
     } 
    } 
    return COLORS.indexOf(v) > -1; 
}; 

new Schema({ 
    color: { type: String, validate: [colorValidator, 'not a valid color'] } 
}); 

我寫的colorValidator迅速幫助你得到一個想法,但你可以很容易地擴展它,以獲得更復雜的顏色驗證。

+0

感謝您的幫助! – Erik 2014-09-13 13:42:30

相關問題