0.3.3 • Published 9 years ago

checkist v0.3.3

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

checkist Build Status

Tool for compose modular and reusable validation functions

Sync, async, mixed, nested, not blocking

Install

npm install --save checkist

Usage

var checkist = require('checkist');
var isString = require('is-string');

var checkString = checkist()
  .use(isString, 'type')
  .use(function (value) {
    return value === 'awesome';
  }, 'content');

checkString(100); // ['type']
checkString('superb'); // ['content']
checkString('awesome'); // null

API

checkist(defaults)

Create new validation function (vf) with default options

.use(fn, path, context)

Add function as validation middleware with specified nested object prop and error context

Can be used validation functions (vf) as middleware

var checkStringType = checkist()
  .use(isString, 'type');

var checkStringLength = checkist()
  .use(hasLength, 'length');

var checkString = checkist()
  .use(checkStringType, 'type')
  .use(checkStringLength, 'length');

var checkObject = checkist()
  .use(checkStringType, 'name', 'nameType');

.exec(value, options, fn)

Validate something via validation middlewares

For sync function:

checkist()
  .use(isObject, 'type')
  .exec({}); // null

For async function:

checkist()
  .use(isObject, 'type')
  .exec({}, function (err) {
    err; // null
  });

.notBlocking()

Start using not blocking middlewares

checkist()
  .use(isObject, 'type')
  .notBlocking()
  .use(hasName, 'name')
  .use(hasEmail, 'email')
  .exec({}, function (err) {
    err; // ['name', 'email']
  });

.nestedErrors()

Push all nested errors in result

var checkEmail = checkist()
  .use(function (value) {
    return 'email' in value;
  }, 'require')
  .use(isEmail, 'format');

var checkUser = checkist()
  .nestedErrors()
  .notBlocking()
  .use(function (value) {
    return 'name' in value;
  }, 'name')
  .use(checkEmail, 'email');

checkUser({}); // ['email', 'email.require', 'name']

vf(value, next)

Alias for vf.exec

Middlewares

Can be used sync:

function mw(value, options) {
  return typeof value === 'string';
}

and async function:

function mw(value, options, next) {
  setTimeout(function () {
    next(typeof value === 'string');
  }, 1000);
}

Example

check-name.js

var checkist = require('checkist');
var isString = require('is-string');

module.exports = checkist()
  .use(isString, 'require')
  .use(function (value) {
    return value.length > 0;
  }, 'length');

check-email.js

var checkist = require('checkist');
var isEmail = require('is-email');
var isString = require('is-string');

module.exports = checkist()
  .use(isString, 'require')
  .use(isEmail, 'format');

check-user.js

var checkist = require('checkist');
var checkEmail = require('check-email');
var checkName = require('check-name');
var isObject = require('is-object');

module.exports = checkist()
  .nestedErrors()
  .use(isObject, 'type')
  .notBlocking()
  .use(checkName, 'name', 'name')
  .use(checkEmail, 'email', 'email');

app.js

var checkUser = require('check-user');
checkUser(undefined); // ['type']
checkUser({}); // ['name', 'name.require', 'email', 'email.require']
checkUser({name: 'awesome', email: 'awesome'}); // ['email', 'email.format']
checkUser({name: 'awesome', email: 'awesome@gmail.com'}); // null

License

MIT

0.3.3

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago