npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tinyvalid

v1.1.4

Published

A simple validation library written in Typescript

Downloads

5

Readme

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)

|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|-|