Permalink
Browse files

Merge pull request #4 from flatiron/fix-is-valid

[resource] .isValid and Resourece#validate
  • Loading branch information...
2 parents e204182 + 9c525ed commit cdec9de228995be3e45bc3d580e0e8eee60bf0dc @indexzero indexzero committed Oct 22, 2011
Showing with 14 additions and 11 deletions.
  1. +14 −11 lib/resourceful/resource.js
@@ -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);
@@ -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);
@@ -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
//
@@ -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 () {

0 comments on commit cdec9de

Please sign in to comment.