1.1.4 • Published 3 years ago

tinyvalid v1.1.4

Weekly downloads
7
License
ISC
Repository
github
Last release
3 years ago

TinyValid

TinyValid is a Typescript written validation library providing various lightweight validation functions.

Table of Contents:

How to use

Install the library by running

npm install --save tinyvalid

You can then import the whole library:

import * from 'tinyvalid';

or you can import only the validators you need and thus reducing your bundle's final size

import { isInteger } from 'tinyvalid/dist/validators/isInteger';
import { isObject } from 'tinyvalid/dist/validators/isObject';
import { isEmptyString } from 'tinyvalid/dist/validators/isEmptyString';

Tip for Typescript projects

Some of the validators make use of type-guards in order to avoid unnecessary type asserting of variables in your code (https://basarat.gitbook.io/typescript/type-system/typeguard#user-defined-type-guards). You can find which validators use type-guards by the orange diamond ( :small_orange_diamond: ) before their name.

Example of type-guards:

const username: string | undefined = Math.random() > .5 ? '   tinyuser  ': undefined;
let trimmedUsername: string;

// PREFER USING VALIDATORS WITH TYPE-GUARDING...

// isString makes use of type-guards which means the typescript can understand after 
// the validator if the variable - username - is of type string or not
if (!isString(username)) {
    console.log('username is not a string');
} else {
    trimmedUsername = username.trim();
}

// ...INSTEAD OF VALIDATORS WITHOUT TYPE-GUARDING

// isNotString does NOT make use of type-guards
if (isNotString(username)) {
    console.log('username is not a string');
} else {

    // You'll have to assert the username's type since the compiler doesn't know after
    // the validator if the variable - username - is a string or undefined
    trimmedUsername = (<string>username).trim();
}

Validators

has

Returns true if the object has a property with the name value (The validator is mostly a wrapper function of Object.prototype.hasOwnProperty)

has (object: any, value: string | number | symbol, nilCheck?: boolean)
ParameterTypeOptionalDefault ValueDescription
objectanyNo-
valuestring|number|symbolNo-
nilCheckbooleanYestrueIf set to true the validator will check first if the object is null or undefined and thus avoiding potential errors

:small_orange_diamond: isArray

Returns true if the value is an array

isArray (value: any)
ParameterTypeOptionalDefault ValueDescription
valueanyNo-

:small_orange_diamond: isArrayOf

Returns true if the value is an array and all of its items match the supplied predicate function

type Predicate: (value: any) => boolean;

isArrayOf (value: any, predicate: Predicate)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-
predicatePredicateNo-A function which will run on each element of the array which takes an element as an argument and returns a boolean

Examples:

import { isArrayOf, isString, isNotEmptyString } from 'tinyvalid';

const arr = ['   ', 'This is a string'];

// Check if the value is an array on strings
// Expected result: true
const arrayOfStrings = isArrayOf(arr, isString);

// Check if the value is an array of non-empty strings
// Expected result: false - since the first element is a string of spaces which after getting
// trimmed by the isNotEmptyString validator will not pass the predicate fuction
const arrayOfNonEmptyStrings = isArrayOf(arr, (val) => isNotEmptyString(val, true));

// Check if the value is an array of strings with less than 20 characters
// Expected Result: true
const arrayOfShortStrings = isArrayOf(arr, (val) => isString(val) && val.length < 20);

:small_orange_diamond: isBool

Returns true if the value is a boolean

isBool (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-

isDecimal

Returns true if the value is a decimal number

isDecimal (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-

isEmpty

Returns true if the value is null or undefined or an empty string or an empty object or an empty array

isEmpty (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-

isEmptyArray

Returns true if the value is an empty array

isEmptyArray (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-

isEmptyObject

Returns true if the value is an empty object

isEmptyObject (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-

isEmptyString

Returns true if the value is an empty string

isEmptyString (value: any, trim?: boolean)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-
trimbooleanYestrueIf set to true the validator will trim the value before checking if it's an empty string

isFinite

Returns true if the value is a finite number

isFinite (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-

isFunc

Returns true if the value is a function

isFunc (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-

isInfinite

Returns true if the value is infinity (positive or negative)

isInfinite (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-

isInteger

Returns true if the value is an integer

isInteger (value: any)

// Shorthand of isInteger
isInt (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-

isNegativeInteger

Returns true if the value is a negative integer

isNegativeInteger (value: any, includeZero?: boolean)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-
includeZerobooleanYesfalseReturns also true if the value is zero

:small_orange_diamond: isNil

Returns true if the value is null or undefined

isNil (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-

isNotArray

Returs true if the value is not an array

isNotArray (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-

isNotEmptyArray

Returns true if the value is not an empty array

isNotEmptyArray (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-

isNotEmptyString

Returns true if the value is not an empty string

isNotEmptyString (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-
trimbooleanYestrueIf set to true the validator will trim the value before checking if it's not an empty string

isNotNull

Returns true if the value is not null

isNotNull (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-

isNotString

Returns true if the value is not a string

isNotString (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-

:small_orange_diamond: isNull

Returns true if the value is null

isNull (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-

:small_orange_diamond: isNumber

Returns true if the value is a number (and not NaN)

isNumber (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-

isObject

Returns true if the value is an object

isObject (value: any, excludeArrays?: boolean)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-
excludeArraysbooleanYestrueIf set to false the validator will count arrays as objects (since in Javascript the typeof an array returns object)

isPositiveInteger

Returns true if the value is a positive integer

isPositiveInteger (value: any, includeZero?: boolean)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-
includeZerobooleanYesfalseReturns also true if the value is zero

:small_orange_diamond: isString

Returns true if the value is a string

isString (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-

:small_orange_diamond: isUndefined

Returns true if the value is undefined

isUndefined (value: any)
ParameterType (Typescript)OptionalDefault ValueDescription
valueanyNo-
1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

0.1.1

4 years ago

0.1.0

4 years ago

1.0.0

4 years ago