Oky
Oky is a JavaScript type testing library.
Installation
Install the stable version:
npm install --save oky
Install validators with oky-validators package
npm install --save oky-validators
Usage
Oky is very simple, you just need to use validate
function with some validators
Validate
/** * Validate * @param {(Validator | Validator[] | object)} validators * @param * @returns {(boolean | string | string[] | object)} Return true when success or errors when failure. * Errors format depends on the validators format : * it returns an array if validators args is an array and object if validators args is an object */;
It takes 2 arguments, validators, and value and returns true
when success and errors
list when failure
validators args can be :
- A validator
;; const validator = isInteger; ; // return true; // return "isIntegerFailed"
- A list of validators
;; const validators = isInteger isPositive; ; // return true; // return ["isIntegerFailed", "isPositiveFailed"]
- A hash with validators
const validators = name: first: isString isPresent last: isString isPresent age: isInteger isPositive isPresent; ;// return true ;/** * return { * name: { * first: ["isStringFailed"] * }, * age: ["isPositiveFailed"] * } */
Create custom Validator
You can create your own validator with validator
function
/** * Create New Validators * @param * @param * @param {string?} errorMessage - Custom error message * @return */createValidatorname: string, func: Function, errorMessage?: string;
func
argument is the function validation :
; const isBlue = ; //=> return true //=> return "isBlueFailed" const isRed = ; //=> return true //=> return "isNotRed"
Currying
validate
is curried by default
;; const checkValidation = ;; // return true 3; // return true;
Validators list
See oky-validators package