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

datamatch

v2.2.6

Published

Simple and Fast Data Validation for Node.js

Downloads

182

Readme

Datamatch - Simple and Fast Data Validation for Node.js

MAJOR UPDATE v2

Datamatch is a user-friendly and easy-to-use JavaScript library for data validation without any dependencies. It allows you to validate input values against specific criteria and returns the validation result. This library is designed for use in Node.js projects. It is perfect for developers who need a fast and reliable way to validate data.

IMPORTANT: Data types and check options are updated every week. Contact issues to add custom data TYPES and OPTIONS.

Table of Contents

Installation

Install Datamatch using npm:

npm i datamatch

back to top

Update

Update Datamatch using npm:

npm install datamatch@latest

back to top

Import

import Datamatch from 'datamatch';

OR

const Datamatch = require('datamatch');

back to top

Usage

Example 1: Number validation

const penCount = 5;
console.log(Datamatch.isNumber(penCount)); // true

back to top

Example 2: Number validation with options

const penCount = 5;
console.log(Datamatch.isNumber(penCount, { max: 4 })); // false

back to top

Example 3: Array validation

const firends = [ `John`, `Katrin`, `Tom` ];
console.log(Datamatch.isArray(firends)); // true

back to top

Example 4: Array validation with options

const firends = [ `John`, `Katrin`, `Tom` ];
console.log(Datamatch.isArray(firends, { maxLength: 2 })); // false

back to top

Example 5: Nested field validation

const obj = {
    one: null,
    two: 55,
    three: { four: 'Hello' },
    five: { six: [ 'John', 'Katrin', 123 ] }
};

const run = Datamatch.init()
    .field('one').isNull()
    .field('two').isNumber()
    .field('three').isObject()
        .field('four').isString()
        .end()
    .field('five').isObject()
        .field('six').isArray().isString().isNumber().end()
        .end()
    .check(obj);

if (run.errors) {
    console.log(run.errors); // Shows all fields path and whats wrong.
} else {
    console.log(true);
}

// Returns: true

IMPORTANT: Always use '.end()' to close .isObject() and .isArray() constructions.

back to top

Example 6: Nested field validation with options

const obj = {
    one: null,
    two: 55,
    three: { four: 'Hello' },
    five: { six: [ 'John', 'Katrin', 123 ] }
};

const run = Datamatch.init()
    .field('one').isNull()
    .field('two').isNumber()
    .field('three').isObject()
        .field('four').isString()
        .end()
    .field('five').isObject()
        .field('six').isArray().isString().isNumber({ max: 122 }).end()
        .end()
    .check(obj);

if (run.errors) {
    console.log(run.errors); // Shows all fields path and whats wrong.
} else {
    console.log(true);
}

// Returns: [ "Array element in field 'five.six' must be less or equal than '122'. '123' given." ]

IMPORTANT: Always use '.end()' to close .isObject() and .isArray() constructions.

back to top

Example 7: AND logic

OPTIONS always means AND logic

For example:

const obj = {
    login: 'john',
};

const run = Datamatch.init()
    .field('login').isString({ minLength: 4, values: ['john', `alex`]})
    .check(obj);

if (run.errors) {
    console.log(run.errors); // Shows all fields path and whats wrong.
} else {
    console.log(true);
}

// Returns: true

Field 'login' must have minimum length 4, AND contain one of values 'john' / 'alex'.

back to top

Example 8: OR logic

TYPES always means OR logic

For example:

const obj = {
    login: 'john',
};

const run = Datamatch.init()
    .field('login').isString()
    .field('password').isUndefined().isString()
    .check(obj);

if (run.errors) {
    console.log(run.errors); // Shows all fields path and whats wrong.
} else {
    console.log(true);
}

// Returns: true

Field 'password' can be do not set, OR set as string.

back to top

Default NodeJS types

isUndefined

Available options: no.

back to top

isNull

Available options: no.

back to top

isBoolean

Available options: no.

back to top

isNumber

Available options:
min
max
length
minLength
maxLength
values
isFloat
isInt
isNumeric

back to top

isBigInt

Available options:
min
max
length
minLength
maxLength
values

back to top

isString

Available options:
minLength
minLength
maxLength
values
isDate
isDomain
isUrl
isHTTPUrl
isHTTPSUrl
isWSUrl
isWSSUrl
isIP
isIPv4
isIPv6
isJSON
isBase64
isFloat
isInt
isNumeric

back to top

isArray

Available options:
length
minLength
maxLength

back to top

isObject

Available options: no.

back to top

isFunction

Available options: no.

back to top

isAsyncFunction

Available options: no.

back to top

isPromise

Available options: no.

back to top

isSymbol

Available options: no.

back to top

isArrayBuffer

Available options: no.

back to top

isSet

Available options: no.

back to top

isMap

Available options: no.

back to top

isDate

Available options: no.

back to top

isRegExp

Available options: no.

back to top

isDataView

Available options: no.

back to top

isInt8Array

Available options: no.

back to top

isInt16Array

Available options: no.

back to top

isInt32Array

Available options: no.

back to top

isUint8Array

Available options: no.

back to top

isUint16Array

Available options: no.

back to top

isUint32Array

Available options: no.

back to top

isFloat32Array

Available options: no.

back to top

isFloat64Array

Available options: no.

back to top

isUint8ClampedArray

Available options: no.

back to top

isSharedArrayBuffer

Available options: no.

back to top

Options

min

console.log(Datamatch.isNumber(5, { min: 5 })); // true
console.log(Datamatch.isNumber(5, { min: 6 })); // false

const ants = BigInt('1000000000000000000000000000000');
console.log(Datamatch.isBigInt(big, { min: '1000000000000000000000000000000' })); // true
console.log(Datamatch.isBigInt(big, { min: '1000000000000000000000000000001' })); // false

back to top

max

console.log(Datamatch.isNumber(5, { max: 5 })); // true
console.log(Datamatch.isNumber(5, { max: 4 })); // false

const atoms = BigInt('3200000000000000000000000000000')
console.log(Datamatch.isBigInt(bigCount, { max: '3200000000000000000000000000000' })); // true
console.log(Datamatch.isBigInt(bigCount, { max: '3199999999999999999999999999999' })); // false

back to top

length

console.log(Datamatch.isNumber(9, { length: 1 })); // true
console.log(Datamatch.isNumber(9, { length: 2 })); // false

console.log(Datamatch.isBigInt(BigInt('12345'), { length: 5 })); // true
console.log(Datamatch.isBigInt(BigInt('12345'), { length: 4 })); // false

console.log(Datamatch.isString('Hello', { length: 5 })); // true
console.log(Datamatch.isString('Hello', { length: 4 })); // false
console.log(Datamatch.isString('Hello', { length: 6 })); // false

console.log(Datamatch.isArray([ 1, 2, 3, 4, 5 ], { length: 5 })); // true
console.log(Datamatch.isArray([ 1, 2, 3, 4, 5 ], { length: 4 })); // false
console.log(Datamatch.isArray([ 1, 2, 3, 4, 5 ], { length: 6 })); // false

minLength

console.log(Datamatch.isNumber(9, { minLength: 1 })); // true
console.log(Datamatch.isNumber(9, { minLength: 2 })); // false

console.log(Datamatch.isBigInt(BigInt('12345'), { minLength: 5 })); // true
console.log(Datamatch.isBigInt(BigInt('12345'), { minLength: 6 })); // false

console.log(Datamatch.isString('Hello', { minLength: 5 })); // true
console.log(Datamatch.isString('Hello', { minLength: 6 })); // false

console.log(Datamatch.isArray([ 1, 2, 3, 4, 5 ], { minLength: 5 })); // true
console.log(Datamatch.isArray([ 1, 2, 3, 4, 5 ], { minLength: 6 })); // false

back to top

maxLength

console.log(Datamatch.isNumber(9, { maxLength: 1 })); // true
console.log(Datamatch.isNumber(9, { maxLength: 0 })); // false

console.log(Datamatch.isBigInt(BigInt('12345'), { maxLength: 5 })); // true
console.log(Datamatch.isBigInt(BigInt('12345'), { maxLength: 4 })); // false

console.log(Datamatch.isString('Hello', { maxLength: 5 })); // true
console.log(Datamatch.isString('Hello', { maxLength: 4 })); // false

console.log(Datamatch.isArray([ 1, 2, 3, 4, 5 ], { maxLength: 5 })); // true
console.log(Datamatch.isArray([ 1, 2, 3, 4, 5 ], { maxLength: 4 })); // false

back to top

values

console.log(Datamatch.isNumber(77, { values: [ 53, 77, 99 ] })); // true
console.log(Datamatch.isNumber(77, { values: [ 53, 99 ] })); // false

console.log(Datamatch.isBigInt(BigInt('77'), { values: [ BigInt('77'), BigInt('99') ] })); // true
console.log(Datamatch.isBigInt(BigInt('77'), { values: [ BigInt('99') ] })); // false

console.log(Datamatch.isString('Hello', { values: [ 'Hello', 'Bye' ] })); // true
console.log(Datamatch.isString('Hello', { values: [ 'Bye' ] })); // false

back to top

isDate (option)

If string value is date (true), you can safely use 'new Date(value)' without fear of errors.

console.log(Datamatch.isString('Thu, 31 Oct 2024 07:28:00 GMT', { isDate: true })); // true
console.log(Datamatch.isString('Thu, 32 Oct 2024 07:28:00 GMT', { isDate: true })); // false

back to top

isDomain

console.log(Datamatch.isString('www.example.com', { isDomain: true })); // true
console.log(Datamatch.isString('example.com', { isDomain: true })); // true
console.log(Datamatch.isString('[email protected]', { isDomain: true })); // false

back to top

isUrl

If string value is url (true), you can safely use '...new URL(value)...' without fear of errors.

console.log(Datamatch.isString('https://www.example.com', { isUrl: true })); // true
console.log(Datamatch.isString('https://example.com', { isUrl: true })); // true
console.log(Datamatch.isString('example.com', { isUrl: true })); // false

back to top

isHTTPUrl

If string value is HTTP url (true), you can safely use '...new URL(value)...' without fear of errors.

console.log(Datamatch.isString('http://www.example.com', { isHTTPUrl: true })); // true
console.log(Datamatch.isString('http://example.com', { isHTTPUrl: true })); // true
console.log(Datamatch.isString('https://example.com', { isHTTPUrl: true })); // false

back to top

isHTTPSUrl

If string value is HTTPS url (true), you can safely use '...new URL(value)...' without fear of errors.

console.log(Datamatch.isString('https://www.example.com', { isHTTPSUrl: true })); // true
console.log(Datamatch.isString('https://example.com', { isHTTPSUrl: true })); // true
console.log(Datamatch.isString('http://example.com', { isHTTPSUrl: true })); // false

back to top

isWSUrl

If string value is WS url (true), you can safely use '...new URL(value)...' without fear of errors.

console.log(Datamatch.isString('ws://www.example.com', { isWSUrl: true })); // true
console.log(Datamatch.isString('ws://example.com', { isWSUrl: true })); // true
console.log(Datamatch.isString('wss://example.com', { isWSUrl: true })); // false

back to top

isWSSUrl

If string value is WSS url (true), you can safely use '...new URL(value)...' without fear of errors.

console.log(Datamatch.isString('wss://www.example.com', { isWSSUrl: true })); // true
console.log(Datamatch.isString('wss://example.com', { isWSSUrl: true })); // true
console.log(Datamatch.isString('ws://example.com', { isWSSUrl: true })); // false

back to top

isIP

console.log(Datamatch.isString('192.168.0.1', { isIP: true })); // true
console.log(Datamatch.isString('2001:0db8:85a3:0000:0000:8a2e:0370:7334', { isIP: true })); // true
console.log(Datamatch.isString('192.168.0.256', { isIP: true })); // false

back to top

isIPv4

console.log(Datamatch.isString('192.168.0.1', { isIPv4: true })); // true
console.log(Datamatch.isString('2001:0db8:85a3:0000:0000:8a2e:0370:7334', { isIPv4: true })); // false
console.log(Datamatch.isString('192.168.0.256', { isIPv4: true })); // false

back to top

isIPv6

console.log(Datamatch.isString('2001:0db8:85a3:0000:0000:8a2e:0370:7334', { isIPv6: true })); // true
console.log(Datamatch.isString('192.168.0.1', { isIPv6: true })); // false
console.log(Datamatch.isString('192.168.0.256', { isIPv6: true })); // false

back to top

isJSON

console.log(Datamatch.isString('{"login":"john"}', { isJSON: true })); // true
console.log(Datamatch.isString('{"login":"john', { isJSON: true })); // false

back to top

isBase64

console.log(Datamatch.isString('SGVsbG8=', { isBase64: true })); // true
console.log(Datamatch.isString('SGVs', { isBase64: true })); // false

back to top

isFloat

If string value is float (true), you can safely use 'parseFloat(value)' without fear of errors.

console.log(Datamatch.isNumber(0.34, { isFloat: true })); // true
console.log(Datamatch.isNumber(2, { isFloat: true })); // false

console.log(Datamatch.isString('0.34', { isFloat: true })); // true
console.log(Datamatch.isString('00.34', { isFloat: true })); // false
console.log(Datamatch.isString('0,34', { isFloat: true })); // false
console.log(Datamatch.isString('2', { isFloat: true })); // false

back to top

isInt

If string value is int (true), you can safely use 'parseInt(value)' without fear of errors.

console.log(Datamatch.isNumber(2, { isInt: true })); // true
console.log(Datamatch.isNumber(0.34, { isInt: true })); // false

console.log(Datamatch.isString('2', { isInt: true })); // true
console.log(Datamatch.isString('02', { isInt: true })); // true
console.log(Datamatch.isString('0.34', { isInt: true })); // false
console.log(Datamatch.isString('0,34', { isInt: true })); // false
console.log(Datamatch.isString('00.34', { isInt: true })); // false

back to top

isNumeric

Contains 'isFloat' + 'isInt' logic.

console.log(Datamatch.isNumber(2, { isNumeric: true })); // true
console.log(Datamatch.isNumber(0.34, { isNumeric: true })); // true
console.log(Datamatch.isNumber(NaN, { isNumeric: true })); // false

console.log(Datamatch.isString('2', { isNumeric: true })); // true
console.log(Datamatch.isString('02', { isNumeric: true })); // true
console.log(Datamatch.isString('0.34', { isNumeric: true })); // true
console.log(Datamatch.isString('0,34', { isNumeric: true })); // false
console.log(Datamatch.isString('00.34', { isNumeric: true })); // false
console.log(Datamatch.isString('NaN', { isNumeric: true })); // false

back to top

License

Datamatch is released under the MIT License.

back to top