Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

credible

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

credible

Unopinionated validation framework for node and the browser.

  • 0.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-72.73%
Maintainers
1
Weekly downloads
 
Created
Source

credible

Build Status

Credible is a library for validating objects in node.js or the browser. While it provides several validators out of the box, credible is very unopinionated; mostly, credible simply provides a framework for object validation.

Installation

Credible has one dependency: An A+ promise library, i.e. bluebird or when.js.

Node.js
$ npm install credible --save

Node takes care of the dependency automatically.

Browser
<script src="/bluebird.js"></script>
<script src="/credible.js"></script>

By default, Credible uses bluebird; to use a different implementation, set Credible.Promise to the correct library.

Usage

var rules = {
  name: {
    presence: true
  },
  email: {
    email: true
  }
}

var obj = {
  name: 'Noah Portes Chaikin',
  email: 'noah.porteschaikin@carrotcreative.com'
}

var credible = new Credible(rules)
  .run(obj)
  .catch(function (errors) {
    console.log(errors.toJSON());
  })
API

Every method in a Chainable instance returns the instance except credible.run(), which returns a promise.

new Credible(arguments..)

The main Credible constructor optionally accepts the same arguments as credible.rule() (see below).

credible.rule(arguments..)

Used to set new rules. credible.rule() is a variadic function; it accepts any of the following sets of arguments:

credible
  .rule(property, validator, options)
  .rule(property, { validator1: options, validator2: options })
  .rule(validator, options)

validator is either an available validator or a function. To use an available validator, simply pass the validator's name as a string or an object key:

credible
  .rule('name', 'presence', true)
  .rule('name', { length: { greaterThan: 5 } })
  .rule({ email: { email: { if: function (obj) { return obj.email; } } } })

On validation, a validator function is passed the object, the property key (if provided), and options. Validator functions can return promises for asynchronous validation. This is an example validator:

var emailValidator = function (object, property, options) {
  if ( /^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,6}$/i.test(object[property])) {
    throw property + ' is an invalid e-mail address';
  }
}

Validators can be sent any number of settings in the options object; the following options are made available to every validator and are handled by the credible object:

KeyDescription
if: fnOnly validate if fn returns true. fn is a function; the object being validated is passed to fn as an argument.
unless: fnOnly validate if fn returns false. fn is a function; the object being validated is passed to fn as an argument.
invalid: fnfn is a function to handle a failed validation; the object being validated is passed to fn as an argument.
credible.if([property], fn)

Only run validator if fn (a function) returns true. fn is passed the object being validated. Optionally, passing property will only execute the test on validators executed on the specified property.

credible
  .if(function (object) {
    return object.foo == 'bar';
  });

credible
  .if('name', function (object) {
    return object.foo == 'bar';
  });
credible.unless([property], fn)

Only run validator if fn (a function) returns false. fn is passed the object being validated. Optionally, passing property will only execute the test on validators executed on the specified property.

credible
  .unless(function (object) {
    return object.foo == 'bar';
  });

credible
  .unless('name', function (object) {
    return object.foo == 'bar';
  });
credible.invalid([property], fn)

Pass fn, a function, for handling a failed validation. fn is passed the object being validated. Optionally, passing property will only execute the function for failed validations executed on the specified property.

credible
  .invalid(function (object) {
    throw 'This object is invalid.';
  });

credible
  .invalid('name', function (object) {
    throw 'This name is invalid.';
  });
credible.run(object)

Run validations on object; returns a promise.

credible
  .run(obj)
  .then(function () {
    console.log('It\'s valid!');
  })
  .catch(function (errors) {
    console.log(errors.toJSON());
  })

Available Validators

length

Property must have a length matching specifications set in options.

Options
KeyDescription
greaterThan: numberProperty must have a length greater than number
lessThan: numberProperty must have a length greater than number
greaterThanOrEqualTo: numberProperty must have a length greater than number
lessThanOrEqualTo: numberProperty must have a length greater than number
equalTo: numberProperty must have a length equal to number
presence

Property must be defined and not empty.

operator

Compare property to a number or another property set in options.

Options
KeyDescription
greaterThan: numberOrPropertyProperty must have a length greater than numberOrProperty
lessThan: numberOrPropertyProperty must have a length greater than numberOrProperty
greaterThanOrEqualTo: numberOrPropertyProperty must have a length greater than numberOrProperty
lessThanOrEqualTo: numberOrPropertyProperty must have a length greater than numberOrProperty
equalTo: numberOrPropertyProperty must have a length equal to numberOrProperty
alpha

Property must contain only letters.

alphaDash

Property must contain only letters and dashes.

alphaNumeric

Property must contain only letters and numbers.

alphaUnderscore

Property must contain only letters and underscores.

email

Property must be a valid e-mail address.

integer

Property must be an integer.

natural

Property must be a positive number.

naturalNonZero

Property must be a positive number greater than zero.

url

Property must be a valid URL.

License & Contributing

Keywords

FAQs

Package last updated on 26 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc