Skip to content
bmeck edited this page Nov 1, 2011 · 2 revisions

Resourceful supports validation via schemas and a validate function.

API

Validation in resourceful is done client side and is exposed by 2 endpoints.

  1. resource.isValid
  2. resource.validate()

Validation MUST be synchronous. This is important because validation is done at multiple points in the life cycle of a resource including updates. To have the ability to cross network boundaries or have long running scripts in order to validate a resource is generally unacceptable and should be made as a method to your resource. Should you need to validate in an asynchronous manner please do so in a way that does not override validate as this can break parts of Resourceful.

Example for Async Validation

When using a resource that requires a schema be validated against an external resource, the preferred way is to do so outside of your resource and put it into your presenter.

function DBDocumentPresenter() {
  this.db = ...; // A place to store files over the network
}
//
// Check if the document is in the db and if the document looks valid
//
DBDocumentPresenter.prototype.validate = function validate(document, callback) {
  var validation = document.validate();
  if(validation.valid) {
    return callback(validation.errors);
  }
  this.db.contains(document.id, callback);
}

This makes more sense than placing it in your resource because it is not related to the document represented by your resource; but other outside factors, in this case, a database that is holding a document represented by this resource.

Clone this wiki locally