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
- Update
- Import
- Usage
-
Default NodeJS types
- isUndefined
- isNull
- isBoolean
- isNumber
- isBigInt
- isString
- isArray
- isObject
- isFunction
- isAsyncFunction
- isPromise
- isSymbol
- isArrayBuffer
- isSet
- isMap
- isDate
- isRegExp
- isDataView
- isInt8Array
- isInt16Array
- isInt32Array
- isUint8Array
- isUint16Array
- isUint32Array
- isFloat32Array
- isFloat64Array
- isUint8ClampedArray
- isSharedArrayBuffer
Installation
Install Datamatch using npm:
npm i datamatch
Update
Update Datamatch using npm:
npm update datamatch --save
Import
import datamatch from 'datamatch';
OR
const datamatch = require('datamatch');
Usage
Example 1: Number validation
const penCount = 5;
console.log(datamatch.isNumber(penCount, { min: 5 })); // true
Example 2: Array validation
const firends = [ `John`, `Katrin`, `Tom` ];
console.log(datamatch.isArray(firends)); // true
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
Example 4: AND + OR
That example means field 'hash':
- Can be a string
- (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
Default NodeJS types
isUndefined
Available options: todo.
isNull
Available options: todo.
isBoolean
Available options: todo.
isNumber
Available options:
min
max
length
minLength
maxLength
values
isFloat
isInt
isNumeric
isBigInt
Available options:
min
max
length
minLength
maxLength
values
isString
Available options:
minLength
minLength
maxLength
values
isDate
isDomain
isUrl
isHTTPUrl
isHTTPSUrl
isWSUrl
isWSSUrl
isIP
isIPv4
isIPv6
isFloat
isInt
isNumeric
isArray
Available options:
length
minLength
maxLength
isObject
Available options: todo.
isFunction
Available options: todo.
isAsyncFunction
Available options: todo.
isPromise
Available options: todo.
isSymbol
Available options: todo.
isArrayBuffer
Available options: todo.
isSet
Available options: todo.
isMap
Available options: todo.
isDate
Available options: todo.
isRegExp
Available options: todo.
isDataView
Available options: todo.
isInt8Array
Available options: todo.
isInt16Array
Available options: todo.
isInt32Array
Available options: todo.
isUint8Array
Available options: todo.
isUint16Array
Available options: todo.
isUint32Array
Available options: todo.
isFloat32Array
Available options: todo.
isFloat64Array
Available options: todo.
isUint8ClampedArray
Available options: todo.
isSharedArrayBuffer
Available options: todo.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
License
Datamatch is released under the MIT License.