dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 376 forks source link

Validations do not seem to be running #538

Closed toddself closed 3 years ago

toddself commented 10 years ago

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.

oriolper commented 10 years ago

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

ascot31 commented 10 years ago

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