Sails 水线模型属性验证类型 'integer'、'float' 失败

Sails Waterline Model attributes Validation type 'integer', 'float' fails

我的模型属性

per: { type: 'float', required: true }, typeid: { type: 'integer', required: true },

我的输入

{ per: '5GH', typeid: '6SD', }

我希望这会失败,并且会收到类似

的错误消息

typeid: [ { rule: 'integer', message: 'undefined should be a integer

但是在验证 o/p 之后验证

{ per: 5, typeid: 6, }

在这种情况下我们需要手动验证整数和浮点数吗?

Official Sails Doc for Validation

如文档中所示,您可以看到整数验证检查整数以及验证字符串。

According to what i have experienced with validation

为了严格验证使用int,decimal代替integer,float

problem with your scene is as follow.

a=5     =>integer in a 
a="5"   =>string but the integer value is 5 
a="5a4" =>string but integer value is 5 not 54

a="a5"  =>string and no integer value.Only this case will fail on validation rule

如果您想根据您的自定义规则严格验证属性,那么您可以在您的 models.See 代码中添加 自定义验证 规则:

module.exports = {
    attributes: {
        name: {
            type:'string'
        },
        mail: {
            type:'string',
            defaultsTo:'a'
        },
        age:{
            type:'string',
            isInt:true
        }
    },
    types:{
        isInt:function(value){
            console.log(value);
            if(typeof value==='number' && value=== Math.floor(value)){
                console.log('value is a number');
                return true;
            }
            if(!isNaN(1*value)){
                console.log(value);
                return true;
            }
            return false;
        }
    }
};

因此,您需要为每个模型编写自定义验证程序。 并且

i guess there is now way currently to write global custom validation rule so that you could apply your validation on attributes of different models by writing validation globally.