okay-js v0.0.13
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 --saveUsage
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)); // falseIn 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)); // trueYou 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 stringCreating 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)); // trueAPI Reference
- okay- .createRule(resolve, param) ⇒ function
- .all(...rules) ⇒ function
- .any(...rules) ⇒ function
- .not(rule) ⇒ function
- .callIf(rule, thenCallback, elseCallback) ⇒ function
- .array() ⇒ function
- .boolean() ⇒ function
- .date() ⇒ function
- .email() ⇒ function
- .empty() ⇒ function
- .eq(param) ⇒ function
- .gt(param) ⇒ function
- .gte(param) ⇒ function
- .lt(param) ⇒ function
- .lte(param) ⇒ function
- .number() ⇒ function
- .object() ⇒ function
- .pattern(param) ⇒ function
- .required() ⇒ function
- .string() ⇒ function
- .taxCode(param) ⇒ function
 
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). Whilevalueandcontextare from the rule invokation,paramis from the creator invokation. Theresolvefunction must return aBooleanvalue. | 
| param | * | It is to be checked against valueaccording to the validation logic expressed byresolvefunction. Ifparamis 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)); // trueokay.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)); // trueokay.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 rulevalidates the value given tocallIf.thenCallbackis invoked with two arguments:(value, context). | 
| elseCallback | function | A function that's invoked if ruledoesn't validate the value given tocallIf.elseCallbackis 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 stringokay.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([])); // trueokay.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)); // trueokay.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())); // trueokay.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')); // trueokay.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 })); // falseokay.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)); // trueokay.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)); // trueokay.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)); // trueokay.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)); // trueokay.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)); // trueokay.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)); // trueokay.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({})); // trueokay.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')); // trueokay.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)); // trueokay.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)); // falseokay.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