datamatch

1.2.5 • Public • Published

Datamatch - Simple and Fast Data Validation for Node.js

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 update datamatch --save

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, { min: 5 })); // true

back to top

Example 2: Array validation

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

back to top

Example 3: Nested field validation

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

const dm = datamatch.init()
    .field('one').isNull()
    .field('two').isNumber({ min: 55 })
    .field('three')
        .field('four').isString({ minLength: 5 }).end()
    .field('five')
        .field('six').isArray()
    .check(obj);

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

// Returns: true

back to top

Example 4: AND + OR

That example means field 'hash':

  1. Can be a string
  2. (can be with length 32 AND not domain) OR (can be with length 64)

'isDomain' - just for example 'AND' logic.

const getTrueOrFalse = () => { return Math.random() >= 0.5 };
const getMD5 = () => { return '191c7d10892d7377d0ca306bc8a96c8b' };
const getSHA256 = () => { return 'd1af65ff329128e24de02418050fc8afca2a626f9f417424aecc5890a6a8f0f5' };

const obj = {
    hash: getTrueOrFalse() ? getMD5() : getSHA256()
};

const dm = datamatch.init()
    .field('hash').isString({ length: 32, isDomain: false }).isString({ length: 64 })
    .check(obj);

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

// Returns: true

back to top

Default NodeJS types

isUndefined

Available options: todo.

back to top

isNull

Available options: todo.

back to top

isBoolean

Available options: todo.

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
isFloat
isInt
isNumeric

back to top

isArray

Available options:
length
minLength
maxLength

back to top

isObject

Available options: todo.

back to top

isFunction

Available options: todo.

back to top

isAsyncFunction

Available options: todo.

back to top

isPromise

Available options: todo.

back to top

isSymbol

Available options: todo.

back to top

isArrayBuffer

Available options: todo.

back to top

isSet

Available options: todo.

back to top

isMap

Available options: todo.

back to top

isDate

Available options: todo.

back to top

isRegExp

Available options: todo.

back to top

isDataView

Available options: todo.

back to top

isInt8Array

Available options: todo.

back to top

isInt16Array

Available options: todo.

back to top

isInt32Array

Available options: todo.

back to top

isUint8Array

Available options: todo.

back to top

isUint16Array

Available options: todo.

back to top

isUint32Array

Available options: todo.

back to top

isFloat32Array

Available options: todo.

back to top

isFloat64Array

Available options: todo.

back to top

isUint8ClampedArray

Available options: todo.

back to top

isSharedArrayBuffer

Available options: todo.

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('example@.com', { isDomain: true })); // false

back to top

isUrl

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

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

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

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

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

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

Package Sidebar

Install

DownloadsWeekly Downloads

10

Version

1.2.5

License

MIT

Unpacked Size

62.4 kB

Total Files

27

Last publish

Collaborators

  • fosper