Question

I am using Sequelize on my Node.JS project and I would like to know if there's a way to use existing validator inside user-defined one.

For example:

  var User = sequelize.define('User', {
    foo: {
      type: Sequelize.STRING,
      validate: {
        newValidator: function (value) {
          //something like 
          if (value.length == 10) {
            return this.foo.isUrl && this.foo.contains('bar');
          } else {
            return this,foo.isEmail;
          }
        }
      }
    }
  });

Is it possible to somehow refer existing validators?

Was it helpful?

Solution

If you are on a pretty new version (not sure when the sequelize validator was exposed to users ) you could do Sequelize.DAOValidator.Validator.isUrl() etc. If that doesn't work for you, try importing validator into your own project var Validator = require('validator'). Since validator is already a sequelize dependency, there should be no need to add it to your package

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top