diff --git a/lib/polyclay.js b/lib/polyclay.js index 2299db8..cfc2e9a 100644 --- a/lib/polyclay.js +++ b/lib/polyclay.js @@ -132,6 +132,13 @@ PolyClay.addType( defaultFunc: function() { return {}; } }); +PolyClay.addType( +{ + name: 'untyped', + validatorFunc: function() { return true; }, + defaultFunc: function() { return null; } +}); + PolyClay.Model.prototype.valid = function() { var i, p, len; @@ -300,6 +307,7 @@ PolyClay.Model.addEnumerableProperty = function(obj, propname, enumerable) PolyClay.Model.addOptionalProperty = function(obj, propname) { obj.prototype.__optional.push(propname); + obj.prototype.__types[propname] = 'untyped'; // No type validation on optional properties. var result = function() @@ -345,13 +353,20 @@ PolyClay.Model.prototype.serialize = function() PolyClay.Model.prototype.propertyTypes = function() { - var result = _.clone(this.__types); - for (var i = 0; i < this.__optional.length; i++) - result[this.__optional[i]] = 'untyped'; + var props = this.__properties; + var result = {}; + + for (var i = 0; i < props.length; i++) + result[props[i]] = this.propertyType(props[i]); return result; }; +PolyClay.Model.prototype.propertyType = function(prop) +{ + return this.__types[prop]; +}; + PolyClay.Model.prototype.update = function(attr) { if (!_.isObject(attr)) diff --git a/test/test-02-persistence.js b/test/test-02-persistence.js index 5c8a1ff..566d80c 100644 --- a/test/test-02-persistence.js +++ b/test/test-02-persistence.js @@ -229,7 +229,6 @@ describe('persistence layer', function() assert.ok(Model.adapter instanceof MockDBAdapter); }); - it('emits before-save', function(done) { var obj = new Model(); @@ -338,7 +337,7 @@ describe('persistence layer', function() }); }); - it('propertyType() returns a hash of types for properties', function() + it('propertyTypes() returns a hash of types for properties', function() { var obj = new Model(); @@ -347,4 +346,14 @@ describe('persistence layer', function() }); + it('propertyType() can query the type of a specific property', function() + { + var obj = new Model(); + + obj.propertyType('key').should.equal('string'); + obj.propertyType('required_prop').should.equal('string'); + obj.propertyType('ephemeral').should.equal('untyped'); + should.not.exist(obj.propertyType('nonexistent')); + }); + });