question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom validation of len not working.

See original GitHub issue

What you are doing?

var Tag = sequelize.define("Tag", {
    name: {
      type: DataTypes.STRING,
      validate: {
        len: {
          args: [[0,32]],
          msg: 'Name too long.'
        }
      }
    }
  }
)

What do you expect to happen?

I’m expection an error message like

errors:
   [ { message: 'Name too long',
       type: 'Validation error',
       path: 'name',
       ...

What is actually happening?

No error is thrown. Nothing gets catched by Sequelize.ValidationError.

Without custom settings it works:

validate: {
        len: [0,32]
      }

throws a validation error.

sequelize: 3.24.3 node: 6.2.2 maria DB (Server version: 10.1.9-MariaDB Homebrew, Protocol version: 10)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sushantdhimancommented, Dec 3, 2016

@ro70 you have been passing arguments incorrectly to the args, it needs to be single array.

From Sequelize docs http://docs.sequelizejs.com/en/v3/docs/models-definition/#validations

Note that where multiple arguments need to be passed to the built-in validation functions, the arguments to be passed must be in an array. But if a single array argument is to be passed, for instance an array of acceptable strings for isIn, this will be interpreted as multiple string arguments instead of one array argument. To work around this pass a single-length array of arguments, such as [[‘one’, ‘two’]] as shown above.

SSCCE Test

var Tag = sequelize.define("Tag", {
  name: {
    type: Sequelize.STRING,
    validate: {
      len: {
        args: [0, 6],
        msg: 'Name too long.'
      }
    }
  }
});

sequelize
  .sync({ force: true })
  .then(() => Tag.create({ name: 'qwertyqwertyqwerty'}))
  .then((t) => console.log(t.toJSON()))
  .catch(console.error);

Output

Using database:  sequelize-test-39
{ SequelizeValidationError: Validation error: Name too long.
    at Promise.all.then (/var/www/github/sequelize/lib/instance-validator.js:76:15)
From previous event:
    at InstanceValidator._validate (/var/www/github/sequelize/lib/instance-validator.js:74:7)
    at modelInstance.constructor.runHooks.then (/var/www/github/sequelize/lib/instance-validator.js:95:16)
From previous event:
    at InstanceValidator.validate (/var/www/github/sequelize/lib/instance-validator.js:94:10)
    at model.validate (/var/www/github/sequelize/lib/model.js:3521:49)
    at Promise.try (/var/www/github/sequelize/lib/model.js:3321:21)
From previous event:
    at model.save (/var/www/github/sequelize/lib/model.js:3318:23)
    at Function.create (/var/www/github/sequelize/lib/model.js:1890:8)
    at sequelize.sync.then (/var/www/github/sequelize/sscce_template.js:26:19)
    at runCallback (timers.js:637:20)
    at tryOnImmediate (timers.js:610:5)
    at processImmediate [as _immediateCallback] (timers.js:582:5)
1reaction
ro70commented, Oct 23, 2016

Here’s the code:

var Sequelize = require('sequelize')
var sequelize = new Sequelize('...', '...', '...')

var Tag = sequelize.define("Tag", {
    name: {
      type: Sequelize.STRING,
      validate: {
        len: { // <- Not throwing an error.
          args: [[0,32]],
          msg: 'Name too long.'
        }
        // len: [0, 32] // <- Throws 'SequelizeValidationError: Validation error: Validation len failed'
      }
    }
  }
)

// Creating values.
sequelize.sync({force: true})
.then(function() {
  return Tag.create(
    {
      name: '0123456789012345678901234567890123456789'
    }
  )
})

// Catching errors...
.catch(function(err) {
  console.log(err)
})

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom validation using IF() and Len() - excel - Stack Overflow
I have been trying for a long time but can not get it by trial and error or googling. My best guess is...
Read more >
Custom validation doesn't work - MSDN
User-2010772455 posted. Hi,. I've wrote this in a view model: [Required(ErrorMessage = "Page title is very important for SEO, please fill in ...
Read more >
Sitecore Forms string length validator not working for other ...
First you need to create an item on /sitecore/system/Settings/Forms/Validations. After you need to create a custom code on Visual studio to ...
Read more >
Custom validation not working on latest version - WordPress.org
We are using Contact Form 7 latest version (5.6.1) on our site and when we try to add custom validation using hooks, it...
Read more >
Validations & Constraints - Sequelize
You can define your custom validators or use several built-in validators, implemented by validator.js (10.11.0), as shown below. sequelize.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found