Schema validation with Joi [Node.js, npmawesome and nodejitsu]

Forked from Hello World for Node.js.

41 Runs 267 Views 9 Copies

Example illustrating how to use Joi schema validation module.

Saved

Saved

alexgorbatchev 17

alexgorbatchev
published 5 months ago

var Joi = require('joi');
var schema;

function log(err, value) {
  value = JSON.stringify(value);

  if (err) {
    console.log('\033[41mFAILED\033[49m ' + value + ' - ' + JSON.stringify(err, null, 2));
  } else {
    console.log('\033[42mOK\033[49m ' + value);
  }
  console.log('\n\n');
}

// most basic example
Joi.string().validate(10, log);
Joi.string().email().validate('hello+gmail.com', log);
Joi.string().email().validate('[email protected]', log);

// more complex example
schema = {counter: Joi.number().min(1).max(10).required()};

Joi.validate({counter: 0}, schema, log);
Joi.validate({counter: 5}, schema, log);

// even more complex example
schema = Joi.object().keys({
    username: Joi.string().alphanum().min(3).max(30).required(),
    password: Joi.string().regex(/[a-zA-Z0-9]{3,30}/),
    confirmation: Joi.ref('password')
  })
  .with('password', 'confirmation')
  ;

Joi.validate({invalidProperty: 1}, schema, log);
Joi.validate({username: 'alex', password: 'qwerty'}, schema, log);
Joi.validate({username: 'alex', password: 'qwerty', confirmation: 'qwerty'}, schema, log);

Terminal

Your session has timed out.

Dismiss (the page may not function properly).