Vawks Clamantis

Screaming at the top of my fingers.

The Joi of Validation

Validation is like flossing: you know you should do it, but it’s easy to skip. And just like flossing, if you neglecting your validation, you’re asking for trouble.

The Joi module is Hapi’s answer to validation, but you don’t need to be using Hapi in order to use Joi. In this tutorial, we’ll show how we can use Joi to validate any object, and then we’ll take a look at how easily we can use Joi to validate requests made to Hapi applications.

Configuring With Confidence

In my previous post on caching with Hapi, I showed this example of how to get your redis cache to work both locally and in production.

var cache_cfg;

if (process.env.REDISTOGO_URL) {
  var rtg = require("url").parse(process.env.REDISTOGO_URL)
  cache_cfg = {
    engine: 'catbox-redis',
    host: rtg.hostname,
    port: rtg.port,
    password: rtg.auth.split(":")[1]
} else {
  cache_cfg = 'catbox-redis';

var server = Hapi.createServer('localhost', 8000, {
  cache: cache_cfg

Then I said that there are “better ways to write environment-specific configuration”.

Well, I wasn’t just selling you a load of salve. There are better ways!

Let’s take at look at Confidence, Hapi’s config module.

Caching With Hapi

In most cases, it’s better to serve old data quickly than to make the user wait for the “correct” data. It’s certainly faster. This is called caching. You want your APIs to be fast? Cache your APIs.

You could probably slap redis or memcached into your existing setup, but what if you’re running an environment like MRI that has to spin up a new process for each request? Or what if you’ve got a number of legacy systems that serve XML, and you want to combine parts of their data into a JSON API for your new mobile app?

Enter hapi.js, a full featured node.js server framework from WalmartLabs. It was first deployed to power under the extreme load of the Black Friday rush.

So yes, I’d say it’s production ready.

Small Grunts

Let’s say you’re a front-end dev who wants to leverage the power of Grunt. You’re making a Wordpress theme, and you want to concatenate and minify your JavaScript files so your site will load faster. We’ll assume that the site is using jQuery, a few jQuery plugins that rely on jQuery, and a site-wide script that relies on both the plugins and jQuery.

How difficult is that for someone who’s proficient in jQuery, maybe has even written a couple plugins, but has never used JS outside the browser?