1.0.4 • Published 3 years ago
fun-validation v1.0.4

JS Functional validation library
Installation
npm install fun-validationyarn add fun-validationUsage
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) => booleanSo 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:
rulesmust have the same shape as the value that was passed in- and consequently the
validationResultwill also be in that same shape
Meaning:
- If the
valueis a primitive (string, number...etc), therulesmust be a validation fn with the signature:(value: any) => boolean, and thevalidationResultwill 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
valueis an object (a plain object), therulesmust an object with the same shape as value but the leaf nodes (primitives) have validation fns as values, and thevalidationResultwill 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
valueis an array, therulesmust 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 thevaluearray. ThevalidationResultwill 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 thevaluearray).
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
rulesmust be of the same shape asvaluewith the leaf nodes (primitives) being functions that look like this:(value: any) => boolean - The
validationResultis more or less of the same shape asvalueandrules(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