Max
Max

Reputation: 71

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

My Model Attributes

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

My input

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

I expect this should fail and will get error message something like

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

But on validation the o/p after validation

{ per: 5, typeid: 6, }

Do we need to manually validate integer and float in this case?

Upvotes: 0

Views: 1876

Answers (1)

vkstack
vkstack

Reputation: 1664

Official Sails Doc for Validation

As in documentation you can see that integer validation check for integer as well as string for validation.

According to what i have experienced with validation

For strictly validate use int,decimal in place of 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

If you want strictly validate the attributes according to you custom rule then you can add custom validation rule in your models.See the code below:

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;
        }
    }
};

So for each model you need to write custom validator. And

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.

enter image description here

enter image description here

Upvotes: 2

Related Questions