Skip to content
Simple arbitrary data validation library
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
example
.gitignore
LICENSE
README.md
index.js
package.json

README.md

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)

Something went wrong with that request. Please try again.