okay-js

0.0.13 • Public • Published

Okay

Okay is a minimalistic functional validation library for Node.js and the browser.

It helps you streamline the validation of arguments, data and forms.

Okay aims to set you free from learning different, framework or environment dependent, convoluted APIs to validate your data. You can run it on the server as well as on the client. You can use it together with any library of your choice.

Okay depends on lodash.

It is tiny (< 1kB).

Installation

npm install okay-js --save

Usage

Okay helps you validate your data using rules.

A rule is a pure function. It accepts a mandatory value argument and an optional context; it always returns a Boolean. If value is a function, it is invoked in order to get the actual value.

Rules are generated by rule creators.

A rule creator is a higher order function that creates a rule. It eventually accepts a single param argument. If param is a function, it is invoked in order to get the actual param. Okay's API mainly exposes rule creators. Also, you can define your own rule creators (and rules).

Let's see an example:

var gt = okay.gt;
var validate = gt(5);
console.log(validate(6)); // true
console.log(validate(5)); // false

In the example above, okay.gt is a rule creator. We call gt(5) in order to create a rule. The rule validates that a given value is greater than 5.

Composing rules

We often need to validate our data against more than one rule. Okay lets you compose rules to perform complex validation:

var gt = okay.gt;
var lt = okay.lt;
var all = okay.all;

var validate = all(gt(0), lt(5));

console.log(validate(-1)); // false
console.log(validate(6)); // false
console.log(validate(3)); // true

You can compose rules using okay.all (logical AND) and okay.any (logical OR) creators.

Perform actions on validation

You can use the rule creator okay.callIf to perform actions on validation.

Let's see an example:

var callIf = okay.callIf;
var string = okay.string;
var thenCallback = function (value) {
  var message = ':value is a string'
    .replace(':value', value);
  console.log(message);
};
var elseCallback = function (value) {
  var message = ':value is not a string'
    .replace(':value', value);
  console.log(message);
};
var validate = callIf(string(), thenCallback, elseCallback);
validate('1'); // 1 is a string
validate(1); // 1 is not a string

Creating custom rules

You can create your own custom rules using okay.createRule

var createRule = okay.createRule;

var resolve = function (value, param) {
  return value != param;
};
var ne = function (param) {
  return createRule(resolve, param);
};

var validate = ne(4);
console.log(validate(3)); // false
console.log(validate(4)); // true

API Reference

okay.createRule(resolve, param) ⇒ function

Creates a rule. Okay's rule creators wrap this method. You can use it to create your own rules.

Kind: static method of okay
Returns: function - The rule function. It has to be invoked with a mandatory value argument and an optional context. It always returns a Boolean.

Param Type Description
resolve function A function defining the validation logic for the rule. It is invoked with three arguments: (value, param, context). While value and context are from the rule invokation, param is from the creator invokation. The resolve function must return a Boolean value.
param * It is to be checked against value according to the validation logic expressed by resolve function. If param is a function, it gets executed in order to get the actual value.

okay.all(...rules) ⇒ function

Rule creator. The created rule checks the given value against all rules and acts as a logical AND.

Kind: static method of okay
Returns: function - The rule function. It has to be invoked with a mandatory value argument and an optional context. It always returns a Boolean.

Param Type
...rules function

Example

var gt = okay.gt;
var lt = okay.lt;
var all = okay.all;
var validate = all(gt(0), lt(5));
console.log(validate(-1)); // false
console.log(validate(6)); // false
console.log(validate(3)); // true

okay.any(...rules) ⇒ function

Rule creator. The created rule checks the given value against all rules and acts as a logical OR.

Kind: static method of okay
Returns: function - the rule function

Param Type
...rules function

Example

var any = okay.any;

okay.not(rule) ⇒ function

Rule creator. The created rule checks the given value against the param rule and acts as a logical NOT.

Kind: static method of okay
Returns: function - the rule function

Param Type
rule function

Example

var required = okay.required;
var not = okay.not;
var validate = not(required());
console.log(validate(null)); // true

okay.callIf(rule, thenCallback, elseCallback) ⇒ function

Rule creator. The created rule checks the given value against rule and calls thenCallback if value is valid. Otherwise, it calls elseCallback.

Kind: static method of okay
Returns: function - The rule function. It has to be invoked with a mandatory value argument and an optional context. It always returns a Boolean.

Param Type Description
rule function
thenCallback function A function that's invoked if rule validates the value given to callIf. thenCallback is invoked with two arguments: (value, context).
elseCallback function A function that's invoked if rule doesn't validate the value given to callIf. elseCallback is invoked with two arguments: (value, context).

Example

var string = okay.string;
var thenCallback = function (value) {
  var message = ':value is a string'
    .replace(':value', value);
  console.log(message);
};
var elseCallback = function (value) {
  var message = ':value is not a string'
    .replace(':value', value);
  console.log(message);
};
var validate = callIf(string(), thenCallback, elseCallback);
validate('1'); // 1 is a string
validate(1); // 1 is not a string

okay.array() ⇒ function

Rule creator. The created rule validates that a value is an array

Kind: static method of okay
Returns: function - the rule function
Example

var array = okay.array;
var validate = array();
console.log(validate({})); // false
console.log(validate([])); // true

okay.boolean() ⇒ function

Rule creator. The created rule validates that a value is a boolean

Kind: static method of okay
Returns: function - the rule function
Example

var boolean = okay.boolean;
var validate = boolean();
console.log(validate(0)); // false
console.log(validate(false)); // true

okay.date() ⇒ function

Rule creator. The created rule validates that a value is a Javascript Date object

Kind: static method of okay
Returns: function - The rule function. It has to be invoked with a mandatory value argument and an optional context. It always returns a Boolean.
Example

var date = okay.date;
var validate = date();
console.log(validate({})); // false
console.log(validate(new Date())); // true

okay.email() ⇒ function

Rule creator. The created rule validates that a value is a valid email address.

Kind: static method of okay
Returns: function - The rule function. It has to be invoked with a mandatory value argument and an optional context. It always returns a Boolean.
Example

var email = okay.email;
var validate = email();
console.log(validate('string')); // false
console.log(validate('user@domain.com')); // true

okay.empty() ⇒ function

Rule creator. The created rule validates that a value is a number.

Kind: static method of okay
Returns: function - The rule function. It has to be invoked with a mandatory value argument and an optional context. It always returns a Boolean..
Example

var object = okay.empty;
var validate = empty();
console.log(validate(null)); // true
console.log(validate(true)); // true
console.log(validate(1)); // true
console.log(validate([1, 2, 3])); // false
console.log(validate({ a: 1 })); // false

okay.eq(param) ⇒ function

Rule creator. The created rule validates that a number is equal to a value.

Kind: static method of okay
Returns: function - The rule function. It has to be invoked with a mandatory value argument and an optional context. It always returns a Boolean.

Param Type Description
param Number | function A number or a function returning a number.

Example

var eq = okay.eq;
var validate = eq(5);
console.log(validate(6)); // false
console.log(validate(5)); // true

okay.gt(param) ⇒ function

Rule creator. The created rule validates that a number is less than or equal to a value.

Kind: static method of okay
Returns: function - The rule function. It has to be invoked with a mandatory value argument and an optional context. It always returns a Boolean.

Param Type Description
param Number | function A number or a function returning a number.

Example

var gt = okay.gt;
var validate = gt(5);
console.log(validate(5)); // false
console.log(validate(6)); // true

okay.gte(param) ⇒ function

Rule creator. The created rule validates that a number is greater than or equal to a value

Kind: static method of okay
Returns: function - The rule function. It has to be invoked with a mandatory value argument and an optional context. It always returns a Boolean.

Param Type Description
param Number | function A value or a function returning a number

Example

var gte = okay.gte;
var validate = gte(5);
console.log(validate(4)); // false
console.log(validate(5)); // true
console.log(validate(6)); // true

okay.lt(param) ⇒ function

Rule creator. The created rule validates that a number is less than a value

Kind: static method of okay
Returns: function - The rule function. It has to be invoked with a mandatory value argument and an optional context. It always returns a Boolean.

Param Type Description
param Number | function A number or a function returning a number

Example

var lt = okay.lt;
var validate = lt(5);
console.log(validate(5)); // false
console.log(validate(4)); // true

okay.lte(param) ⇒ function

Rule creator. The created rule validates that a number is less than or equal to a value

Kind: static method of okay
Returns: function - The rule function. It has to be invoked with a mandatory value argument and an optional context. It always returns a Boolean.

Param Type Description
param Number | function A number or a function returning a number.

Example

var lte = okay.lte;
var validate = lte(5);
console.log(validate(6)); // false
console.log(validate(5)); // true
console.log(validate(4)); // true

okay.number() ⇒ function

Rule creator. The created rule validates that a value is a number.

Kind: static method of okay
Returns: function - The rule function. It has to be invoked with a mandatory value argument and an optional context. It always returns a Boolean..
Example

var object = okay.number;
var validate = number();
console.log(validate('a')); // false
console.log(validate(1)); // true

okay.object() ⇒ function

Rule creator. The created rule validates that a value is an object.

Kind: static method of okay
Returns: function - The rule function. It has to be invoked with a mandatory value argument and an optional context. It always returns a Boolean..
Example

var object = okay.object;
var validate = object();
console.log(validate(1)); // false
console.log(validate({})); // true

okay.pattern(param) ⇒ function

Rule creator. The created rule validates that a value matches a regular expression.

Kind: static method of okay
Returns: function - The rule function. It has to be invoked with a mandatory value argument and an optional context. It always returns a Boolean.

Param Type Description
param RegExp | function A regular expression or a function returning a regular expression.

Example

var pattern = okay.pattern;
var validate = pattern(/[0-9]/);
console.log(validate('a')); // false
console.log(validate('1')); // true

okay.required() ⇒ function

Rule creator. The created rule validates that a value is non-blank.

Kind: static method of okay
Returns: function - The rule function. It has to be invoked with a mandatory value argument and an optional context. It always returns a Boolean..
Example

var required = okay.required;
var validate = required();
console.log(validate(null)); // false
console.log(validate(void 0)); // false
console.log(validate('')); // false
console.log(validate(false)); // true

okay.string() ⇒ function

Rule creator. The created rule validates that a given value is a string.

Kind: static method of okay
Returns: function - The rule function. It has to be invoked with a mandatory value argument and an optional context. It always returns a Boolean..
Example

var string = okay.string;
var validate = string();
console.log(validate('1')); // true
console.log(validate(1)); // false

okay.taxCode(param) ⇒ function

Rule creator. The created rule validates that a given value is a tax code.

Kind: static method of okay
Returns: function - The rule function. It has to be invoked with a mandatory value argument and an optional context. It always returns a Boolean.

Param Type Description
param String | function A ISO 3166 alpha-2 country code or a function returning a ISO 3166 alpha-2 country code

Example

var taxCode = okay.taxCode;
var validate = taxCode('it');
console.log(validate('DGVDRN78E02H501C')); // true

Package Sidebar

Install

npm i okay-js

Weekly Downloads

0

Version

0.0.13

License

MIT

Last publish

Collaborators

  • adigiovanni