Geddy models/validations for Node.js

The last couple of weeks have been a blast.

I've been settling into my new job at Yammer, getting to know the guys on the team, and enjoying the insanely delicious food. Yes, we have a chef that comes in; it's pretty ridiculous.

By happy accident, my team at Yammer is using the work I've done with Geddy on models and validations -- but in the browser.

It's kind of mind-bending to see code written for Node.js running in IE6, but that's a real-world example of the pure awesome we get with server-side JavaScript. No more writing the exact same input-validation in two different languages.

Geddy has an easy, intuitive way of defining models, with a full-featured set of data validations. The syntax is very similar to models in Ruby's ActiveRecord or DataMapper, or Python's SQLAlchemy.

As I mentioned, the model module works great both in Node, and in the browser, so it's very easy to share model and input validation code in your app between client and server.

Here is an example of a model with some validations:

    var
   
   User =
   
    function
   
   () {
   
    this
   
   .property(
   
    'login'
   
   ,
   
    'String'
   
   , {required:
   
    true
   
   });
   
    this
   
   .property(
   
    'password'
   
   ,
   
    'String'
   
   , {required:
   
    true
   
   });
   
    this
   
   .property(
   
    'lastName'
   
   ,
   
    'String'
   
   );
   
    this
   
   .property(
   
    'firstName'
   
   ,
   
    'String'
   
   );
   
    this
   
   .validatesPresent(
   
    'login'
   
   );
   
    this
   
   .validatesFormat(
   
    'login'
   
   , /[a-z]+/,
      {message:
   
    'Subdivisions!'
   
   });
   
    this
   
   .validatesLength(
   
    'login'
   
   , {min:
   
    3
   
   });
   
    this
   
   .validatesConfirmed(
   
    'password'
   
   ,
   
    'confirmPassword'
   
   );
   
    this
   
   .validatesWithFunction(
   
    'password'
   
   ,
   
    function
   
   (s) {
   
    // Something that returns true or false
   
   
    return
   
   s.length >
   
    0
   
   ;
  });
   
    // Can define methods for instances like this
   
   
    this
   
   .someMethod =
   
    function
   
   () {
   
    // Do some stuff
   
   };
};
   
    // Can also define them on the prototype
   
   User.prototype.someOtherMethod =
   
    function
   
   () {
   
    // Do some other stuff
   
   };
   
    // Server-side, commonjs
   
   exports.User = User;
   
    // Client-side
   
   
    // model.registerModel('User');
   
  
 

Creating an instance of one of these models is easy:

    var
   
   params = {
  login:
   
    'alex'
   
   ,
  password:
   
    'lerxst'
   
   ,
  lastName:
   
    'Lifeson'
   
   ,
  firstName:
   
    'Alex'
   
   };
   
    var
   
   user = User.create(params);
  
 

Data-validation happens on the call to create , and any validation errors show up inside an errors property on the instance, keyed by field name. Instances have a valid method that returns a Boolean indicating whether the instance is valid.

    // Leaving out the required password field
   
   
    var
   
   params = {
  login:
   
    'alex'
   
   ,
};
   
    var
   
   user = User.create(params);
   
    // Prints 'false'
   
   sys.puts(user.valid());
   
    // Prints 'Field "password" is required'
   
   sys.puts(user.errors.password);
  
 

I've been writing a bunch of tests so you can see some other examples.

Really cool seeing code that runs equally well on the server and in the browser. A credible server-side JavaScript plaform like Node really opens up a ton of new possibilities. Looking forward to seeing where it all goes.


Comments

::: {} Kuroki Kaze (2010-04-27)

::: {} Very interesting :) Haven't looked into this part of Geddy yet.


:::::