fun-validation
v1.0.4
Published
🥳 JS Functional validation library
Downloads
4
Readme
JS Functional validation library
Installation
npm install fun-validation
yarn add fun-validation
Usage
This library exports a bunch of small validation functions that all have the following signature:
(value: any) => boolean
// or when the validation requires additional parameters, we have currying:
(n:number) => (value: any) => boolean
So you would call a function like this:
import { isEmail } from 'fun-validation';
const isEmailValid = isEmail(someString);
The library also exports the validate
function.
This function has the following signature:
(value: any, rules) => validationResult;
Where:
rules
must have the same shape as the value that was passed in- and consequently the
validationResult
will also be in that same shape
Meaning:
- If the
value
is a primitive (string, number...etc), therules
must be a validation fn with the signature:(value: any) => boolean
, and thevalidationResult
will be a boolean
import { validate, isString, isInteger, isNumberMax } from 'fun-validation';
const someString = 'this is a string';
validate(someString, isString); // returns boolean
// or, you can compose your own validation fn
const someNumber = 3;
validate(someNumber, value => isInteger(value) && isNumberMax(5)(value)); // returns boolean
- If the
value
is an object (a plain object), therules
must an object with the same shape as value but the leaf nodes (primitives) have validation fns as values, and thevalidationResult
will be and object in the same shape
import { validate, isString } from 'fun-validation';
import { isDate } from 'date-fns';
const user = {
name: 'Dusan',
dateOfBirth: new Date(1992, 9, 2),
};
const rules = {
name: isString,
dateOfBirth: isDate,
};
const result = validate(user, rules);
// result
{
name: boolean;
dateOfBirth: boolean;
}
- If the
value
is an array, therules
must an array with two elements. First element is a validation fn and is used to validate the array itself (for example length of the array). The second element corresponds to the shape of the element in thevalue
array. ThevalidationResult
will be an array with the first element as boolean (result of validating the array itself), and the second element as an array of booleans (result of validation for each of the elements in thevalue
array).
import { validate, isArray, isString, isInteger } from 'fun-validation';
// array of primitives
const hobbies = ['tennis', 'basketball'];
const rules = [isArray, isString];
const result = validate(hobbies, rules);
// result
[true, [true, true]];
// array of objects
const friends = [
{ name: 'Dusan', age: 29 },
{ name: 'Peter', age: 33 },
];
const rules = [isArray, { name: isString, age: isInteger }];
const result = validate(friends, rules);
// result
[
true,
[
{ name: true, age: true },
{ name: true, age: true },
],
];
You get the picture!
So, to recapitulate:
- The
rules
must be of the same shape asvalue
with the leaf nodes (primitives) being functions that look like this:(value: any) => boolean
- The
validationResult
is more or less of the same shape asvalue
andrules
(except in the case of arrays) while the leaf nodes are always booleans
API
const validate: (value: any, rules) => validationResult
const isString: (value: any): boolean;
const isStringLongerThan: (len: number) => (value: any) => boolean
const isStringShorterThan:(len: number) => (value: any) => boolean
const isStringOfLength: (len: number) => (value: any) => boolean
const isStringOfMinLength: (len: number) => (value: any) => boolean
const isStringOfMaxLength:(len: number) => (value: any) => boolean 7
const isFunction: (obj: any) => boolean
const isObject: (value: any) => boolean
const isPromise: (value: any) => boolean
const isArray: (value: any) => boolean
const isArrayLongerThan: (len: number) => (value: any) => boolean
const isArrayShorterThan: (len: number) => (value: any) => boolean
const isArrayOfLength: (len: number) => (value: any) => boolean
const isArrayMinLength: (len: number) => (value: any) => boolean
const isArrayMaxLength: (len: number) => (value: any) => boolean
const isNumber: (value: any) => boolean
const isInteger: (value: any) => boolean
const isFloat: (value: any) => boolean
const isNumberMoreThan: (n: number) => (value: any) => boolean
const isNumberLessThan: (n: number) => (value: any) => boolean
const isNumberEqual: (n: number) => (value: any) => boolean
const isNumberMin: (n: number) => (value: any) => boolean
const isNumberMax: (n: number) => (value: any) => boolean
const isPattern: (regex: RegExp) => (value: any) => boolean
const isEmail: (value: any) => boolean
const isUrl: (value: any) => boolean