Skip to content

Commit

Permalink
Merge pull request #4 from flatiron/fix-is-valid
Browse files Browse the repository at this point in the history
[resource] .isValid and Resourece#validate
  • Loading branch information
indexzero committed Oct 22, 2011
2 parents e204182 + 9c525ed commit cdec9de
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions lib/resourceful/resource.js
Expand Up @@ -160,12 +160,8 @@ Resource.create = function (attrs, callback) {
attrs.ctime = attrs.mtime = Date.now()
}

var instance = new(this)(attrs);

// Before we create a new resource, perform a validation based on its schema
var validate = validator.validate(instance._properties, {
properties: this.schema.properties || {}
});
var instance = new(this)(attrs),
validate = this.prototype.validate(instance, this.schema);

if (!validate.valid) {
return callback(validate.errors);
Expand All @@ -182,10 +178,7 @@ Resource.create = function (attrs, callback) {
};

Resource.save = function (obj, callback) {
// Before we save a resource, perform a validation based on its schema
var validate = validator.validate(obj, {
properties: this.schema.properties || {}
});
var validate = this.prototype.validate(obj, this.schema);

if (!validate.valid) {
return callback(validate.errors);
Expand Down Expand Up @@ -481,6 +474,16 @@ Resource.sync = function (callback) {
});
};

Resource.prototype.validate = function(instance, schema) {
instance || (instance = this);
schema || (schema = this.schema || {});

// Before we create a new resource, perform a validation based on its schema
return validator.validate(instance._properties, {
properties: schema.properties || {}
});
};

//
// Prototype
//
Expand Down Expand Up @@ -540,7 +543,7 @@ Resource.prototype.__defineGetter__('id', function () {
});

Resource.prototype.__defineGetter__('isValid', function () {
return true;
return this.validate().valid;
});

Resource.prototype.__defineGetter__('properties', function () {
Expand Down

0 comments on commit cdec9de

Please sign in to comment.