Giter VIP home page Giter VIP logo

hygiene.js's Introduction

Hygiene.js - sanitize your input

Build Status

Asynchronous validator

Hygiene.js is written for NodeJS, so it's only natural for it to be asyncronous. The point of Hygiene.js validator is to validate user input objects. It's convenient when using web frameworks like express etc.

###Usage

var hygiene = require('hygiene');

var userValidator = hygiene.validator()
	.withString('name')
	.withNumber('age');
	
userValidator({name: 'John Doe', age: 'Not a number'}, function(err, result, resultDetails) {
	assert.equal(false, result);
	assert.equal("Property 'age' is of wrong type", resultDetails.age);
});

Asychronous Sanitizer

NodeJS makes it easy to use document databases that support storing JSON objects. However it doesn't remove the need to sanitize what goes to the database and in which format. Hygiene.js helps you with keeping your data - the most valuable part of your application - clean and easy to use.

###Usage

var hygiene = require('hygiene');

var userValidator = hygiene.validator()
	.withString('name')
	.withNumber('age');
	
userValidator({name: 'John Doe', age: '33'}, function(err, result, resultDetails, sanitizedObject) {
	assert.equal(true, result);
	assert.strictEqual(33, sanitizedObject.age);
});

Custom validators

So, what about custom validators? Well, just pass it to hygiene. This is why it is so handy to be asynchronous

var h = require('hygiene'),
    http = require('http');
var val = h.validator().with('twitter', {validator: function(options, value, messages, callback) {
  http.get('http://api.twitter.com/1/users/show.xml?screen_name=' + value, function(res) {
    if (res.statusCode == 404) {
      return callback(undefined, 'Twitter handle ' + value + ' not found');
    }
    if (res.statusCode >= 400) {
      return callback(new Error('Request failed'), null);
    }
    return callback(undefined, null);
  }).on('error', function(e) {
    return callback(e, null);
  });
}});

val({twitter: 'nnarhinen'}, function(err, result, resultDetails) {
  if (err) throw err;
  assert.equal(true, result);
});

val({twitter: 'nnarhinenshouldnotbefound'}, function(err, result, resultDetails) {
  if (err) throw err;
  assert.equal(false, result);
  assert.deepEqual({twitter: 'Twitter handle nnarhinenshouldnotbefound not found'}, result.resultDetails);
});

hygiene.js's People

Stargazers

Marcin Bogusz avatar Michael Reyes avatar Juho Vepsäläinen avatar Mikko Ohtamaa avatar Niklas Närhinen avatar Alexis Delrieu avatar Antoine Pintout avatar Esa-Matti Suuronen avatar Marcello Prattico avatar

Watchers

Alex Gadea avatar Niklas Närhinen avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.