diff --git a/lib/Instance.js b/lib/Instance.js index 4e398e4..e8bcedb 100644 --- a/lib/Instance.js +++ b/lib/Instance.js @@ -172,10 +172,10 @@ Instance.prototype.__extendSchema = function() { schema[property] = false; for(var property in this.__state.model.schema) - schema[property] = this.__state.model.schema[property]; + if(schema[property]) delete schema[property]; for(var targetProperty in schema) { - if(!$.hasOwnProperty(targetProperty)) + if(!$[targetProperty] && !$.hasOwnProperty(targetProperty)) (function(targetProperty) { Object.defineProperty($, targetProperty, { get: function() { @@ -215,6 +215,20 @@ Instance.forModel = function(model) { var proto = {}; + _.each(model.schema, function(validator, name) { + Object.defineProperty(proto, name, { + get: function() { + return this.__state.modified[name] === undefined ? null : this.__state.modified[name]; + }, + set: function(value) { + var validation = validate(validator, value, name, model.extraValidators); + if(!validation.passed) throw validation.toError(); + this.__state.modified[name] = value; + }, + enumerable: true + }); + }); + _.each(model.options.virtuals, function(property, name) { if('function' == typeof property) Object.defineProperty(proto, name, { diff --git a/test/findOne.js b/test/findOne.js index 1974fdf..eba0559 100644 --- a/test/findOne.js +++ b/test/findOne.js @@ -48,7 +48,7 @@ describe('orm', function () { model.findOne({ name: 'Demo1' }, function(err, obj) { if(err) return done(err); should.exist(obj); - obj.should.have.ownProperty('name', 'Demo1'); + obj.should.have.property('name', 'Demo1'); done(); }); }); @@ -57,7 +57,7 @@ describe('orm', function () { model.findOne('Demo1', function(err, obj) { if(err) return done(err); should.exist(obj); - obj.should.have.ownProperty('name', 'Demo1'); + obj.should.have.property('name', 'Demo1'); done(); }); }); @@ -96,7 +96,7 @@ describe('orm', function () { model.findOne({ name: 'Demo1' }, function(err, obj) { if(err) return done(err); should.exist(obj); - obj.should.have.ownProperty('name', 'Demo1'); + obj.should.have.property('name', 'Demo1'); d1instance = obj; done(); }); @@ -106,8 +106,8 @@ describe('orm', function () { model.findOne(d1instance.id, function(err, obj) { if(err) return done(err); should.exist(obj); - obj.should.have.ownProperty('id').with.type('string'); - obj.should.have.ownProperty('name', 'Demo1'); + obj.should.have.property('id').with.type('string'); + obj.should.have.property('name', 'Demo1'); done(); }); }); diff --git a/test/insertion.js b/test/insertion.js index 5d5c33d..f04c0b5 100644 --- a/test/insertion.js +++ b/test/insertion.js @@ -41,7 +41,7 @@ describe('orm', function () { name: 'Demo1' }, function(err, instance) { if(err) return done(err); - instance.should.have.ownProperty('name', 'Demo1'); + instance.should.have.property('name', 'Demo1'); return done(); }); }); @@ -62,8 +62,8 @@ describe('orm', function () { }], function(err, instances) { if(err) return done(err); should(Array.isArray(instances)); - instances[0].should.have.ownProperty('name', 'Demo2'); - instances[1].should.have.ownProperty('name', 'Demo3'); + instances[0].should.have.property('name', 'Demo2'); + instances[1].should.have.property('name', 'Demo3'); return done(); }); }); diff --git a/test/instance.js b/test/instance.js index b956e1d..9c0d9bb 100644 --- a/test/instance.js +++ b/test/instance.js @@ -162,9 +162,9 @@ describe('orm', function () { i.age = 'hello'; }).should.throwError(); - i.should.have.ownProperty('id', 'custom_id'); - i.should.have.ownProperty('name', 'name'); - i.should.have.ownProperty('age', null); + i.should.have.property('id', 'custom_id'); + i.should.have.property('name', 'name'); + i.should.have.property('age').and.eql(null); }); }); });