JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 137541
  • Score
    100M100P100Q149560F
  • License MIT

Simple validations for node and the browser.

Package Exports

  • checkit

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (checkit) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

CheckIt

Build Status

Simple validations for node.js and the browser

The CheckIt library aims to be a lightweight, flexible, validation library, with no dependence on the DOM, targeting both Node.js and the browser.

CheckIt depends on underscore.js, the underscore.function.predicates.js of underscore-contrib and (optionally) when.js for using the library asynchronously with promises. If you wish to use when, but would rather use browser globals than a package manager, a shimmed version of when is included in the /lib directory for your convenience.

Getting Started

Creating a CheckIt object starts with specifying a target to be validated, as well as an optional options object to help setup the validation settings such as language and async.

var validator  = CheckIt(target, options);

Validating an object

The run method passes through to runAsync or runSync depending on whether the async flag is set globally or in the options passed to the CheckIt object.

validator.run(validations);

Methods:

setLabels(labels)

Applies the labels for the current validation values, so error messages aren't weird looking and such.

applyToAll(rules)

Takes a rule, or array of rules to be applied against each item in the validation target.

run([rules])

The rules are optional, particularly

Validation options:

If no language is specified, then the value of CheckIt.defaultLanguage will be used (defaults to "en").

Errors:

The CheckIt.Error object is used to handle all errors. If a validation is run synchronously, the validation will return false and this value will be set to the .validationError property on the currently validating instance. If the validation is run asynchronously, this error will be passed in rejecting the promise.

CheckIt.Error Methods

get(key)

Gets the array of validation error messages for a particular key off the validation object.

first(key)

Gets the first of validation error messages for a particular key.

toJSON([all])

Turns the validation errors into a hash. If the optional all is set to true, then it returns an array of messages rather than the first validation message for each error key.

toString()

A string saying how many errors have been triggered total in the current validation.


Example:

var example = {
  'user' : 'Joe User',
  'email' : 'joe@example.com',
  'password' : '123',
  'password_confirm' : '456'
};

Example 1: Simple Validation

CheckIt(example).run({
  'user'     : ['required', 'alphaDash', 'maxLength:255'],
  'email'    : ['required', 'validEmail'],
  'password' : ['required']
  'password_confirm': ['matchesField:password']
}).then(function(validator) {

}, function(err) {

});