node-orm2 icon indicating copy to clipboard operation
node-orm2 copied to clipboard

Validations not working?

Open DimitarDechew opened this issue 7 years ago • 1 comments

I'm trying to validate the input data but when I do console.log(err) I'm receiving 'null'

Code:

  <!-- models.js -->
  var cluster = db.define('cluster', {
    name : String,
   }, {
     validations: {
       name: db.enforce.notEmptyString('Oops! Your cluster needs a name.'),
       name: db.enforce.unique({ ignoreCase : true }, 'Oops! Cluster with that name already exist.')
     }
  });

 <!-- Cluster Controller -->
 //TODO: Make validations for the cluster using Node-ORM2
  add: function(m, db, socket) {

    var cluster = new db.models.cluster({ name : m.name, shop_id : m.shop, display_id : m.display });
    cluster.save(function(err) {
      console.log(err)
    })
  }

DimitarDechew avatar Jun 24 '18 08:06 DimitarDechew

     validations: {
       name: db.enforce.notEmptyString('Oops! Your cluster needs a name.'),
       name: db.enforce.unique({ ignoreCase : true }, 'Oops! Cluster with that name already exist.')
     }

It seems the only thing you are checking is uniqueness, not length because you are passing an object, not an array, so only the last validation will be applied as keys can't repeat in an object definition. If you would like two validators for the same field, please see the doco here: https://github.com/dresende/node-orm2/wiki/Model-Validations

dxg avatar Jun 25 '18 13:06 dxg