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

Validations do not seem to be running

Open toddself opened this issue 12 years ago • 2 comments

ORM Version: 2.1.16 Driver: mysql 2.3.2 and sqlite 2.2.4 Platform: OS X 10.9.4 Node: 0.10.28 NPM: 1.4.9

Validations don't seem to preventing bad models from saving What I expected to happen: err to be returned from either #save or #create when calling those methods with a model that does not contain valid data.

orm.enforce.ranges.length(1 ,undefined, 'missing') as the validator provides the same effect as below in the test case.

Test case:

var orm = require('orm');
var assert = require('assert');
var settings = { host: 'test-db', database: 'test', protocol: 'sqlite' };

orm.connect(settings, function(e, db){
  db.define(
    'test',
    {name: {type: 'text'}},
    {validations: {name: orm.enforce.notEmptyString('missing')}}
  );

  db.sync(function(err){
    db.models.test.create({}, function(err, testObj){
      assert.ok(testObj);
      assert.ok(!err);
    });

    var testObj = new db.models.test({});

    testObj.save(function(err){
      assert.ok(!err);
     }); 
  });
});
> sqlite3 test-db
SQLite version 3.7.13 2012-07-17 17:46:21
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from test;
|1
|2

It is completely possible that I am providing the validations wrong to the model or some other crazy thing that I missed in the documentation, but I have been unable to find it.

toddself avatar Jul 25 '14 21:07 toddself

I experienced the same issue with orm.enforce.required(). Btw orm.enforce.unique() works perfectly.

oriolper avatar Aug 01 '14 15:08 oriolper

Hi, You need add alwaysValidate if you field is not "required"

ascot31 avatar Aug 05 '14 02:08 ascot31