如何知道哪个属性称为水线验证规则?

Jam*_*Ham 7 validation models sails.js waterline

我在某些字段上进行自己的自定义验证,因此只接受某些值(取决于字段),其余的被拒绝.我想写一个"过滤器"函数,它检查称为验证的属性,并从那里决定允许使用哪个单词.所以模型看起来像这样:

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
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

当然,在正常的Sails.js行为中," attribute"不是属性,而是属性的值.(这attribute.value只是一个例子,意思是,我想要那里的属性值).

所以,我想attribute成为调用验证规则的实际属性.这可能与Sails有关吗?我的意思是,我可以为模型中的每个字段编写一个函数,但是拥有一个适合所有字段的函数会很好(我有很多这样的函数).

谢谢.

acl*_*ve1 1

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

attributes: {
  state: {
    type: 'string',
    enum: ['pending', 'approved', 'denied']
  }
}
Run Code Online (Sandbox Code Playgroud)

我会假设这段代码只是一个人为的示例。这是我认为可行的方法。

attributes: {
  state: {
    type: 'string',
    enum: ['pending', 'approved', 'denied']
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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