admin管理员组

文章数量:1345016

I am trying to validate min and max validations through model validations

last_name:{
            type:Sequelize.STRING,
            validate:{
                notEmpty:{
                    args:true,
                    msg:"Last name required"
                },
                is:{
                    args:["^[a-z]+$",'i'],
                    msg:"Only letters allowed in last name"
                },
                max:{
                    args:32,
                    msg:"Maximum 32 characters allowed in last name"
                },
                min:{
                    args:4,
                    msg:"Minimum 4 characters required in last name"
                }
            }
        }

But the min and max validators are never fired all other validators are working fine

I am trying to validate min and max validations through model validations

last_name:{
            type:Sequelize.STRING,
            validate:{
                notEmpty:{
                    args:true,
                    msg:"Last name required"
                },
                is:{
                    args:["^[a-z]+$",'i'],
                    msg:"Only letters allowed in last name"
                },
                max:{
                    args:32,
                    msg:"Maximum 32 characters allowed in last name"
                },
                min:{
                    args:4,
                    msg:"Minimum 4 characters required in last name"
                }
            }
        }

But the min and max validators are never fired all other validators are working fine

Share asked Jun 16, 2017 at 7:33 JabaaJabaa 1,7637 gold badges36 silver badges63 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

You need to pass args an array

    max:{
         args:[32],
         msg:"Maximum 32 characters allowed in last name"
   },
   min:{
        args:[4],
        msg:"Minimum 4 characters required in last name"
   }

With use of len validator:

var Test = sequelize.define('test', {
     name: {
         type: Sequelize.STRING,
         validate: {
             notEmpty: {
                 args: true,
                 msg: "Required"
            },
            is: {
                args: ["^[a-z]+$", 'i'],
                msg: "Only letters allowed"
            },
            len: {
                args: [4,32],
                msg: "String length is not in this range"
           }
       }
    },
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
   }
}, {
tableName: 'test'
});        

  Test.create({name: "ab"}, function(error, result) {});

本文标签: javascriptSequelizejs min and max length validator are not workingStack Overflow