DateValidator – Simple Validation for Node.js

While I’m passionate about user experience, I also spend a lot of time working on various development projects as well.  Recently, I’ve focused most of my efforts behind Node.js and have enjoyed it thoroughly.  The amount of passion and adoption I see around the language astounds me since the language was created just back in 2009.  Though, since Node is written on top of JavaScript, it feels like a much more mature language.  Sadly though, JavaScript isn’t perfect and is missing a lot of common features that other languages have.  Thankfully, the Node community has helped in these shortcomings though the use of NPM, Node’s package management system.

One of the shortcomings that I found on a recent project was the lack of a simple date validator for ensuring that a date is valid for a provided day, month, and year. There are a few packages on NPM that would suffice; however, they were huge and ultimately too large if I wanted to share them with the user’s web browser.  So I decided to create my own and share it on NPM.

DateValidator (npm, github) is just a simple date validation module that works both in Node.js on the server as well as in a client’s browser.  It isn’t fancy at the moment; however, provides a simple way to validate things like a date of birth or free form date fields.

Installation

Installing DateValidator is simple thanks to NPM.  To install it, simply type the following command.

npm install DateValidator

Using with Node.js

Once the package is installed using NPM, using the DateValidator is as simple as importing in the module into your code.

var date_validator = require("DateValidator").DateValidator;
var is_valid = date_valdiator.validate("2012", "02", "31"); // returns false

Using with JavaScript

One goal I had when writing DateValidator was to ensure that it can be used both client and server side. Being able to use it in both places ensure that you can write your validation code once and reuse it wherever.

UPDATE – 09 Aug. 2012: the module has been updated to utilize NPM more to make client-side installation easier than manually copying the script from node_modules or referencing directly from node_modules. Typing in the following command will prompt you for a client-side installation directory and then will automatically copy the script to your JavaScript source directory.

npm run-script DateValidator client-install

To use the DateValidator in object from a client’s browser, you simply have to reference the script using a typical <script> tag and then use pretty much the same as above.

 
<script type="text/javascript" src="path/to/date-validator.js"></script>
<script type="text/javascript">
var date_validator = window.DateValidator;
var is_valid = date_valdiator.validate("2012", "02", "31"); // returns false
</script>

Global Objects Are Bad

I’m not a fan of adding objects into the global scope (or window object) in my JavaScript code. Inside of the date-validator.js class, I have allowed for a namespace to be generated and used if the developer wishes to do such.  If namespace remains undefined, the DateValidator object will be placed into the global scope.

To add a namespace for the client-side implementation of DateValidator, simply modify the first line of date-validator.js like the following:

window.MyNamespace = window.MyNamespace || {};
var namespace = window.MyNamespace;

Now, when it’s being consumed by the client side JavaScript, it’d be referenced in the following manner.

<script type="text/javascript">
var date_validator = window.DateValidator;
var is_valid = date_valdiator.validate("2012", "02", "31"); // returns false
</script>

Summary

The DateValidator module is still in it’s early stages and I’m always looking for feedback.  If you have any suggestions or find any bugs, please submit them to the issue tracker over on github and I’ll see what I can do to get them updated.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s