lawmaster

Simple arbitrary data validation library.

npm install lawmaster
1 downloads in the last day
1 downloads in the last week
2 downloads in the last month

lawmaster.js

Lawmaster is a simple general purpose validation module that can be used in any situation where data needs to be validated and return information about the errors present.

Example Usage

var validate = require('lawmaster');

var phoneNumber = '(123) 456-7890';
var phoneValidators = [presence, string, phone];

validate(phoneNumber, phoneValidators, function(errors) {
  if (errors !== true) {
    console.log(phoneNumber+' has the following errors:');
    for (var error in errors) {
      console.log("  - "+errors[error]);
    }
  } else {
    console.log('All validators passed.');
  }
});

function presence(data) {
  if ((data !== undefined) || (data !== ''))
    return true;
  else
    return 'Data is not present.';
}

function string(data) {
  if (typeof(data) === 'string')
    return true;
  else
    return 'Data is not a string.'
}

function phone(data) {
  if (/^\+?\s*?1?\s*?\(?\d{3}\)?-?\s*?\d{3}-?\d{4}$/.test(data))
    return true;
  else
    return 'Data is not a phone number.';
}

License

Copyright (c) 2012, Joe Rozner (MIT)

npm loves you