SkillAgentSearch skills...

Checkit

simple, flexible validations for node and the browser

Install / Use

/learn @tgriesser/Checkit
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

Checkit.js

A DOM-independent validation library for Node.js, io.js and the browser.

It supports both sync

It allows you to seamlessly validate full javascript objects, defining custom messages, labels, and validations, with full support for asynchronous validations with promises. It supports conditional validations, and has powerful, consistent error structuring and utility methods for manipulating your errors' output any way you can imagine.

var mainRules = Checkit(rules);

mainRules
  .run(obj)
  .then(function(validatedFields) {
    console.log('The fields: ' + _.keys(validatedFields).join(', ') + ' were validated!');
  })
  .caught(Checkit.Error, function(err) {
    $("#errors").html(err.map(function(val, key) {
      return '<li>' + key + ': ' + val.first().message + '</li>';
    }).join(''));
  });

Node.js

npm install checkit

Browser

The easiest way to use the library is with webpack or browserify

API:

Checkit(validators, [options])

The main Checkit constructor may be called with or without the new keyword, taking a hash of fields/rules for these fields to be validated.

Options:

language

Used to specify the default language key for using a particular language file, currently en, es, ru and fr are supported.

labels

Specifies labels for use in error messages for specific keys

messages

Adds specific messages for individual errors

Example:

checkit.run

checkit.validate (alias)

var checkit = new Checkit({
  firstName: 'required',
  lastName: 'required',
  email: ['required', 'email']
});

var body = {
  email: 'test@example.com',
  firstName: 'Tim',
  lastName: 'Griesser',
  githubUsername: 'tgriesser'
};

checkit.run(body).then(function(validated) {
  console.log(validated);
}).catch(Checkit.Error, function(err) {
  console.log(err.toJSON());
})

checkit.runSync

checkit.validateSync (alias)

var checkit = new Checkit({
  firstName: 'required',
  lastName: 'required',
  email: ['required', 'email']
});

var body = {
  email: 'test@example.com',
  firstName: 'Tim',
  lastName: 'Griesser',
  githubUsername: 'tgriesser'
};

var [err, validated] = checkit.validateSync(body)

// ...

Checkit.check(key, value, rules)

Checkit.check('email', email, ['required', 'email'])
  .catch(function(err) {
    console.log(err.message)
  });

Checkit.checkSync(key, value, rules)

// ES6...
var [err, resp] = Checkit.checkSync('email', email, ['required', 'email'])  

if (err) {

} else {
  // ...
}

Available Validators

<table> <thead> <tr> <th style="min-width:250px;">Validation Name</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td>accepted</td> <td>The value must be yes, on, or 1. This is useful for validating "Terms of Service" acceptance.</td> </tr> <tr> <td>alpha</td> <td>The value must be entirely alphabetic characters.</td> </tr> <tr> <td>alphaDash</td> <td>The value may have alpha-numeric characters, as well as dashes and underscores.</td> </tr> <tr> <td>alphaNumeric</td> <td>The value must be entirely alpha-numeric characters.</td> </tr> <tr> <td>alphaUnderscore</td> <td>The value must be entirely alpha-numeric, with underscores but not dashes.</td> </tr> <tr> <td>arguments</td> <td>The value must be a javascript "arguments" object.</td> </tr> <tr> <td>array</td> <td>The value must be a valid array object.</td> </tr> <tr> <td>base64</td> <td>The value must be a base64 encoded value.</td> </tr> <tr> <td>between:min:max</td> <td>The value must have a size between the given min and max.</td> </tr> <tr> <td>boolean</td> <td>The value must be a javascript boolean.</td> </tr> <tr> <td>contains:value</td> <td>The value must contain the value. For a string, it does an "indexOf" check, an array "_.indexOf" and for an object "_.has".</td> </tr <tr> <td>date</td> <td>The value must be a valid date object.</td> </tr> <tr> <td>different:fieldName</td> <td>The given field must be different than the `fieldName` under validation.</td> </tr> <tr> <td>email</td> <td>The field must be a valid formatted e-mail address.</td> </tr> <tr> <td>empty</td> <td>The value under validation must be empty; either an empty string, an empty, array, empty object, or a falsy value.</td> </tr> <tr> <td>exactLength:value</td> <td>The field must have the exact length of "val".</td> </tr> <tr> <td>exists</td> <td>The value under validation must not be undefined.</td> </tr> <tr> <td>finite</td> <td>The value under validation must be a finite number.</td> </tr> <tr> <td>function</td> <td>The value under validation must be a function.</td> </tr> <tr> <td>greaterThan:value</td> <td>The value under validation must be "greater than" the given value.</td> </tr> <tr> <td>greaterThanEqualTo:value</td> <td>The value under validation must be "greater than" or "equal to" the given value.</td> </tr> <tr> <td>integer</td> <td>The value must have an integer value.</td> </tr> <tr> <td>ipv4</td> <td>The value must be formatted as an IPv4 address.</td> </tr> <tr> <td>ipv6</td> <td>The value must be formatted as an IPv6 address.</td> </tr> <tr> <td>lessThan:value</td> <td>The value must be "less than" the specified value.</td> </tr> <tr> <td>lessThanEqualTo:value</td> <td>The value must be "less than" or "equal to" the specified value.</td> </tr> <tr> <td>luhn</td> <td>The given value must pass a basic luhn (credit card) check regular expression.</td> </tr> <tr> <td>matchesField:fieldName</td> <td>The value must exactly match the value of another `fieldName` under validation.</td> </tr> <tr> <td>max:value</td> <td>The value must be less than a maximum value. Strings, numerics, and files are evaluated in the same fashion as the size rule.</td> </tr> <tr> <td>maxLength:value</td> <td>The value must have a length property which is less than or equal to the specified value. Note, this may be used with both arrays and strings.</td> </tr> <tr> <td>min:value</td> <td>The value must have a minimum value. Strings, numerics, and files are evaluated in the same fashion as the size rule.</td> </tr> <tr> <td>minLength:value</td> <td>The value must have a length property which is greater than or equal to the specified value. Note, this may be used with both arrays and strings.</td> </tr> <tr> <td>NaN</td> <td>The value must be <tt>NaN</tt>.</td> </tr> <tr> <td>natural</td> <td>The value must be a natural number (a number greater than or equal to 0).</td> </tr> <tr> <td>naturalNonZero</td> <td>The value must be a natural number, greater than or equal to 1.</td> </tr> <tr> <td>null</td> <td>The value must be <tt>null</tt>.</td> </tr> <tr> <td>number</td> <td>The value must be a javascript <tt>Number</tt>.</td> </tr> <tr> <td>numeric</td> <td>The value must have a numeric value.</td> </tr> <tr> <td>object</td> <td>The value must pass an <tt>_.isObject</tt> check.</td> </tr> <tr> <td>plainObject</td> <td>The value must be an object literal.</td> </tr> <tr> <td>regExp</td> <td>The value must be a javascript <tt>RegExp</tt> object.</td> </tr> <tr> <td>required</td> <td>The value must be present in the input data.</td> </tr> <tr> <td>string</td> <td>The value must be a string type.</td> </tr> <tr> <td>url</td> <td>The value must be formatted as an URL.</td> </tr> <tr> <td>uuid</td> <td>Passes for a validly formatted UUID.</td> </tr> </tbody> </table>

Conditional Validations

Sometimes you may wish to require a given field conditionally, for example require a field only if another field has a greater value than 100. Or you may need two fields to have a given value only when another field is present. Adding these validation rules doesn't have to be a pain. First, create a Checkit instance with the main rules that never change:

var checkit = new Checkit({
  firstName: ['required'],
  lastName: ['required'],
  email: ['required', 'email']
});

Then use the maybe method to add additional rules:

.maybe(rules, handler)

The first of the maybe method is the hash of validation fields / settings, similar to the main Checkit object. The second argument is a function, evaluated with the object being validated as the first argument and the context passed to run (if any) as a second argument. If it returns explicitly true or with a promise fulfilling with true, it will add an additional validator to the Checkit object.

This method makes building complex conditional validations a snap.

// In this example, the "authorBio" field is only required if there are
// more than 5 books specified in the input object
checkit.maybe({authorBio: ['required', 'max:500']}, function(input) {
  return input.books > 5;
});
// In this example, the "authorBio" field is only required if specified by the context
checkit.maybe({authorBio: ['required', 'max:500']}, function(input, context) {
  return context.requireBio;
});

checkit.run(someObj, { requireBio: tr
View on GitHub
GitHub Stars221
CategoryDevelopment
Updated1mo ago
Forks50

Languages

JavaScript

Security Score

95/100

Audited on Feb 27, 2026

No findings