我正在对某些字段进行自定义验证,以便仅接受特定值(取决于字段),其余的则被拒绝。我想编写一个“过滤器”函数,检查哪个属性调用了验证,并从那里决定该属性允许使用哪些单词。因此,模型将如下所示:
module.exports = {
types: {
filter: function(attribute) {
if (attribute === 'number') {
switch(attribute.value) {
case 'one':
return true;
case 'two':
return true;
default:
return false;
}
} else if (attribute === 'color') {
switch(attribute.value) {
case 'red':
return true;
case 'blue':
return true;
default:
return false;
}
}
},
},
attributes: {
number: {
type: 'string',
required: true,
filter: true
},
color: {
type: 'string',
required: true,
filter: true
}
}
};
当然,在正常的Sails.js行为中,"
attribute
"不是属性,而是属性的值。(而attribute.value
只是一个例子,意思是我想在里面得到属性值)。所以,我希望
attribute
是实际调用验证规则的属性。Sails能做到吗?我的意思是,我可以为模型中的每个字段编写一个函数,但为所有字段编写一个适合它们的函数会很好(我有很多字段)。谢谢。