最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Sequelize.js min and max length validator are not working - Stack Overflow

matteradmin5PV0评论

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) {});
Post a comment

comment list (0)

  1. No comments so far