Validation 如何知道称为吃水线验证规则的属性?

Validation 如何知道称为吃水线验证规则的属性?,validation,sails.js,models,waterline,Validation,Sails.js,Models,Waterline,我正在对某些字段进行自己的自定义验证,以便只接受某些值(取决于字段),而拒绝其他值。我想编写一个“filter”函数来检查哪个属性称为验证,并从中决定允许该属性使用哪些词。所以这个模型看起来像这样: module.exports = { types: { filter: function(attribute) { if (attribute === 'number') { switch(attribute.va

我正在对某些字段进行自己的自定义验证,以便只接受某些值(取决于字段),而拒绝其他值。我想编写一个“filter”函数来检查哪个属性称为验证,并从中决定允许该属性使用哪些词。所以这个模型看起来像这样:

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中可能吗?我的意思是,我可以为模型中的每个字段编写一个函数,但是如果有一个适合所有字段的函数(我有很多字段)就好了


谢谢。

好的,我会回答你的问题,但这可能不是你想要的。属性可以有一个“枚举”,这是我们实现最终目标的方式:

attributes: {
  state: {
    type: 'string',
    enum: ['pending', 'approved', 'denied']
  }
}
但是我假设这段代码只是一个虚构的例子。我认为这是一种可行的方法

module.exports={
类型:{
过滤器:函数(属性){
如果(属性=='number'){
开关(属性值){
案例一:
返回true;
案例二:
返回true;
违约:
this.validationErrors.push(属性);
返回false;
}
}else if(属性==='color'){
开关(属性值){
案例“红色”:
返回true;
“蓝色”案例:
返回true;
违约:
this.validationErrors.push(属性);
返回false;
}
}
},
},
属性:{
validationErrors:(函数(){
var错误=[];
返回{
推送:功能(attr){
错误。推送(attr);
},
get:function(){
返回错误;
}
};
})(),
编号:{
键入:“字符串”,
要求:正确,
过滤器:真
},
颜色:{
键入:“字符串”,
要求:正确,
过滤器:真
}
}
};