如何确定哪个属性调用了水位线验证规则?

7

我正在对某些字段进行自定义验证,以便仅接受特定值(取决于字段),其余的则被拒绝。我想编写一个“过滤器”函数,检查哪个属性调用了验证,并从那里决定该属性允许使用哪些单词。因此,模型将如下所示:

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能做到吗?我的意思是,我可以为模型中的每个字段编写一个函数,但为所有字段编写一个适合它们的函数会很好(我有很多字段)。
谢谢。
1个回答

1

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

attributes: {
  state: {
    type: 'string',
    enum: ['pending', 'approved', 'denied']
  }
}

但是我会假设这段代码只是一个编造的例子。以下是我认为可行的一种方法。

    module.exports = {
      
        

        types: {

            filter: function(attribute) {

                if (attribute === 'number') {
                    switch(attribute.value) {

                        case 'one':
                            return true;

                        case 'two':
                            return true;

                        default:
                            this.validationErrors.push(attribute);
                            return false;

                    }
                } else if (attribute === 'color') {
                    switch(attribute.value) {

                        case 'red':
                            return true;

                        case 'blue':
                            return true;

                        default:
                            this.validationErrors.push(attribute);
                            return false;

                    }
               }

            },


        },

        attributes: {
            validationErrors:(function(){
              
              var errors = [];
              
              return {
                push:function(attr){
                  errors.push(attr);
                },
                get:function(){
                  return errors;
                }
              };
              
            })(),

            number: {
                type: 'string',
                required: true,
                filter: true
            },

            color: {
                type: 'string',
                required: true,
                filter: true
            }
        }
    };

编辑:使用属性方法而不是属性

这个答案可能存在一些问题。我不确定waterline如何处理这些自定义类型函数中的"this"。 "this"绑定到模型还是我们正在创建的模型实例?这里有很多问题需要问,但也许这可以给你一些想法并引发讨论。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接