tinyvalid
v1.1.4
Published
A simple validation library written in Typescript
Downloads
5
Maintainers
Readme
TinyValid
TinyValid is a Typescript written validation library providing various lightweight validation functions.
Table of Contents:
- How to use
- Tip for Typescript projects
- Validators:
- has
- :small_orange_diamond: isArray
- :small_orange_diamond: isArrayOf
- :small_orange_diamond: isBool
- isDecimal
- isEmpty
- isEmptyArray
- isEmptyObject
- isEmptyString
- isFinite
- isFunc
- isInfinite
- isInteger / isInt
- isNegativeInteger
- :small_orange_diamond: isNil
- isNotArray
- isNotEmptyArray
- isNotEmptyString
- isNotNull
- isNotString
- :small_orange_diamond: isNull
- :small_orange_diamond: isNumber
- isObject
- isPositiveInteger
- :small_orange_diamond: isString
- :small_orange_diamond: isUndefined
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)
|Parameter|Type|Optional|Default Value|Description| |--|--|--|--|--| |object|any|No|-| |value|string|number|symbol|No|-| |nilCheck|boolean|Yes|true|If 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)
|Parameter|Type|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
: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)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-| |predicate|Predicate|No|-|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)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
isDecimal
Returns true if the value is a decimal number
isDecimal (value: any)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
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)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
isEmptyArray
Returns true if the value is an empty array
isEmptyArray (value: any)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
isEmptyObject
Returns true if the value is an empty object
isEmptyObject (value: any)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
isEmptyString
Returns true if the value is an empty string
isEmptyString (value: any, trim?: boolean)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-| |trim|boolean|Yes|true|If 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)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
isFunc
Returns true if the value is a function
isFunc (value: any)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
isInfinite
Returns true if the value is infinity (positive or negative)
isInfinite (value: any)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
isInteger
Returns true if the value is an integer
isInteger (value: any)
// Shorthand of isInteger
isInt (value: any)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
isNegativeInteger
Returns true if the value is a negative integer
isNegativeInteger (value: any, includeZero?: boolean)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-| |includeZero|boolean|Yes|false|Returns also true if the value is zero
:small_orange_diamond: isNil
Returns true if the value is null or undefined
isNil (value: any)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
isNotArray
Returs true if the value is not an array
isNotArray (value: any)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
isNotEmptyArray
Returns true if the value is not an empty array
isNotEmptyArray (value: any)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
isNotEmptyString
Returns true if the value is not an empty string
isNotEmptyString (value: any)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-| |trim|boolean|Yes|true|If 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)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
isNotString
Returns true if the value is not a string
isNotString (value: any)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
:small_orange_diamond: isNull
Returns true if the value is null
isNull (value: any)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
:small_orange_diamond: isNumber
Returns true if the value is a number (and not NaN)
isNumber (value: any)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
isObject
Returns true if the value is an object
isObject (value: any, excludeArrays?: boolean)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-| |excludeArrays|boolean|Yes|true|If 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)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-| |includeZero|boolean|Yes|false|Returns also true if the value is zero
:small_orange_diamond: isString
Returns true if the value is a string
isString (value: any)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|
:small_orange_diamond: isUndefined
Returns true if the value is undefined
isUndefined (value: any)
|Parameter|Type (Typescript)|Optional|Default Value|Description| |--|--|--|--|--| |value|any|No|-|