November, 2010


24
Nov 10

How to do Prototype Validation in Javascript

I hacked together a quick model for a prototype REST API written in Node.js, and wanted to validate an object prior to saving it to a database. I didn’t want or need some wacky full blown ORM but I did want to ensure that the records I was storing were consistent.

Validating in Javascript can be done in many ways but this is a method I came up with after a couple iterations. Let’s take a look at the code.

PictureValidations = function() {
 
  this.name = function(name) {
    return /^[A-Za-z0-9_-]{8,64}$/.test(name);
  }
 
  this.type = function(type) {
    return /^(jpeg|png|gif|bmp)$/.test(type);
  }
 
  this.tags = function(tags) {
    for (i in tags) {
      if(!(/^[A-Za-z0-9]+$/.test(tags[i]))) { return false };
    }
  }
 
}
 
exports.Picture = function(options) {
  this.name =  options.name ||"unassigned" ;
  this.tags = options.tags;
  this.description = options.description;
  this.created_on = new Date().getTime();
}
 
exports.Picture.prototype.valid = function() {
  var validations = new PictureValidations();
  var valid = true;
  for (v in validations) {
    valid = validations[v](this[v]);
    if (valid == false) { break ; } // stop checking on first failure
  };
  return valid
}

How it works

We’ve defined a new Object called Picture which has all the properties associated with it, as well as some defaults.

We also specify another object called PictureValidations which contains a bunch of functions. Each function is named after the properties in the Picture that we want to validate. Each function will return either True or False when it is called with the valid() method which has been added to the Picture prototype. The key is the line that says:


valid = validations[v](this[v]);

This is storing the status returned from the function in the validations Object we’ve instantiated with the value of the current objects properties (this).
It will then check to see whether or not it passed or failed.

The nice thing about this method is that you can write a function that does just about anything to determine whether or not something is valid.

Anyway, it seems to work, but I’d appreciate any feedback!


23
Nov 10

How to learn Node.js from Scratch

Server-side Javascript has appealed to me for quite a awhile, and while watching from the sidelines was interesting, I still never had the time to really get my feet wet and see how the language really worked. That changed after joining Joyent, of course. We’re the proud home of Node.js, and it would be wrongĀ unforgivableĀ if I didn’t know how to write JS.

It turns out that learning Javascript has been quite fun, and really easy. There are numerous resources on the internet for learning how to manipulate the DOM, and do ridiculous stuff with JQuery. But I’ve never been a UI person. I’m a server and network guy, through and through. It turns out, the hardest part about learning Javascript is finding decent documentation. Most of my learning experience has been through reading other peoples wacky, sometimes incomprehensible code, and then going through lists of tutorials that I wrote for myself. Breaking things seems to be the best way to learn how to write stuff. I also was able to rely on some of my brilliant co-workers who are full time software engineers.

Here’s a list of resources I found helpful, which don’t necessarily show up at the top of your Google search results.

NodeTuts

A decent list of tutorials as screencasts. They can be a little on the long side, but there are a few gems. Check out the one about learning how Sys.pump (util.pump) works. Once you understand how it works you’ll kind of be blown away at how easily you wrote something so powerful

Node.Js Documentation

The API documentation for node is extremely well written, a testament to Ryan’s Benevolent Dictator for Life mentality, which is a really good thing.

Crockford on Javascript

Douglas Crockford has a ton of really well written examples, standards, “best practices”, and videos. If you’re interested, check out “Javascript – The Good Parts” post, hosted on the Yahoo! developer network (Mirror please…)

Mozilla Developer Network Javascript Reference

I don’t know why this wasn’t the first result I found when I searched for things like “Javascript Object” or “Javascript Array”, but this is the best resource that explains what the native objects are, what methods they include, and provides some decent examples on their usage. A MUST read.

Learning Advanced Javascript

John Resig is putting together a book on the Secrets of Javascript, and it has something to do with Ninjas. But this part of the book is available for people to read today, and it’s pretty cool. After you nail down a lot of the style, prototyping, and closure stuff, be sure to check out this list of examples on advanced JS.