o-validator

Flexible and lightweight object validation library.


Keywords
validate, validator, validation, predicates, objects, schema, json
License
ISC
Install
npm install o-validator@1.0.1

Documentation

o-validator

Build Status Coverage Status

Simple and functional object validator.

Validate objects with common predicate functions. No special syntax required.

Install

$ npm install o-validator --save

Run the specs

$ npm test

Usage

var Validator = require('o-validator'),
    R         = require('ramda');

var pattern = {
  title       : Validator.required(R.is(String))
  description : R.allPass([R.is(String), hasLengthGreaterThan(5)]),
  isActive    : R.is(Boolean),
  tags        : R.is(Array)
};

Validator.validate(pattern, {
  title       : 'Hi There',
  description : 'This is a great post.',
  isActive    : true
  // tags are not defined - but that is valid, validator treats them as optional
});
// => true

The validator runs each argument against the defined validation pattern, asserting a true outcome for each. Properties defined in the validation pattern are assumed to be optional unless declared otherwise using Validator.required.

Note: this module is best used with a functional library to provide predicates (isString, isNull, etc.), such as ramda.

All function are curried

All methods in this library are curried, which means that if a method is called without all the arguments it requires, the return value will be a function that takes the remaining arguments. This is super helpful in general, and in the case of this library it makes it possible to create validator functions that can be saved and run at a later time.

For example, one could write the previous validation like this:

var Validator = require('o-validator');

var validadeArgs = Validator.validate(<pattern>);

validadeArgs(<args>);
// => Boolean

Available Methods

As noted previously, all methods in the library are curried. The type signatures are written to reflect that.

Note: the type Predicate is defined as a function that takes any value and returns a boolean ((a -> Boolean)).

Validator.validate

{k: Predicate} -> {k: a} -> Boolean

Validates arguments against the provided pattern.

Validator.validate(<pattern>, <args>) -> Boolean

Validator.getErrors

{k: Predicate} -> {k: a} -> [Object]

Returns a list of errors for a validation pattern with values. Errors are objects with information about the original call. If no errors are found, the method returns an empty array.

Validator.getErrors(<pattern>, <args>) -> [Object]

// Error object is in the form of:
// {
//   property  : String,
//   errorCode : String,
//   message   : String
// }

Validator.validateOrThrow

{k: Predicate} -> {k: a} -> Error or {k: a}

Throws an error if any predicate returns false, otherwise returns the original input arguments.

// Invalid args
Validator.validateOrThrow(<pattern>, <args>) -> Error

// Valid args
Validator.validateOrThrow(<pattern>, <args>) -> <args>

Validator.required

Predicate -> {predicate: Predicate, required: true}

Returns an object that specifies the predicate and that the value is required. This should be used to denote that a property is required, since otherwise properties as assumed to be optional.

var validateArgs = Validator.validate({
  title       : Validator.required(isString)
  description : isString
});

// when the validator is invoked, a title property must be supplied,
// while the description property is optional

TODO

  • Add ability to insert custom error messages, and/or create custom error messages for custom predicates.