datamatch
v2.2.16
Published
Simple and Fast Data Validation for Node.js
Downloads
35
Maintainers
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
- isUndefined
- isNull
- isBoolean
- isNumber
- isBigInt
- isString
- isArray
- isObject
- isFunction
- isAsyncFunction
- isPromise
- isSymbol
- isArrayBuffer
- isSet
- isWeakSet
- isMap
- isWeakMap
- isWeakRef
- isDate
- isRegExp
- isDataView
- isInt8Array
- isInt16Array
- isInt32Array
- isUint8Array
- isUint16Array
- isUint32Array
- isFloat32Array
- isFloat64Array
- isUint8ClampedArray
- isSharedArrayBuffer
- isFinalizationRegistry
- isAbortController
Installation
Install Datamatch using npm:
npm i datamatch
Update
Update Datamatch using npm:
npm install datamatch@latest
Import
import Datamatch from 'datamatch';
OR
const Datamatch = require('datamatch');
Usage
Example 1: Number validation
const penCount = 5;
console.log(Datamatch.isNumber(penCount)); // true
Example 2: Number validation with options
const penCount = 5;
console.log(Datamatch.isNumber(penCount, { max: 4 })); // false
Example 3: Array validation
const firends = [ `John`, `Katrin`, `Tom` ];
console.log(Datamatch.isArray(firends)); // true
Example 4: Array validation with options
const firends = [ `John`, `Katrin`, `Tom` ];
console.log(Datamatch.isArray(firends, { maxLength: 2 })); // false
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.
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.
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'.
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.
Default NodeJS types
isUndefined
Available options: no.
isNull
Available options: no.
isBoolean
Available options: no.
isNumber
Available options:
min
max
length
minLength
maxLength
values
isFloat
isInt
isNumeric
isBigInt
Available options:
min
max
length
minLength
maxLength
values
isString
Available options:
contains
containsOnly
isBase64
isDate
isDomain
isE164
isEmail
isFloat
isHTTPSUrl
isHTTPUrl
isInt
isIP
isIPv6
isIPv4
isJSON
isMd5
isNumeric
isSha256
isUrl
isWSSUrl
isWSUrl
length
maxLength
minLength
values
isArray
Available options:
length
minLength
maxLength
isObject
Available options: no.
isFunction
Available options: no.
isAsyncFunction
Available options: no.
isPromise
Available options: no.
isSymbol
Available options: no.
isArrayBuffer
Available options: no.
isSet
Available options: no.
isWeakSet
Available options: no.
isMap
Available options: no.
isWeakMap
Available options: no.
isWeakRef
Available options: no.
isDate
Available options: no.
isRegExp
Available options: no.
isDataView
Available options: no.
isInt8Array
Available options: no.
isInt16Array
Available options: no.
isInt32Array
Available options: no.
isUint8Array
Available options: no.
isUint16Array
Available options: no.
isUint32Array
Available options: no.
isFloat32Array
Available options: no.
isFloat64Array
Available options: no.
isUint8ClampedArray
Available options: no.
isSharedArrayBuffer
Available options: no.
isFinalizationRegistry
Available options: no.
isAbortController
Available options: no.
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('[email protected]', { isDomain: true })); // false
isE164
console.log(Datamatch.isString('+74923341293', { isE164: true })); // true
console.log(Datamatch.isString('84923341293', { isE164: true })); // false
isEmail
console.log(Datamatch.isString('[email protected]', { isEmail: true })); // true
console.log(Datamatch.isString('[email protected]', { isEmail: true })); // false
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
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
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
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
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
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
isJSON
console.log(Datamatch.isString('{"login":"john"}', { isJSON: true })); // true
console.log(Datamatch.isString('{"login":"john', { isJSON: true })); // false
isMd5
console.log(Datamatch.isString('42a718d6317dad85d3ea6ef6389d7db0', { isMd5: true })); // true
console.log(Datamatch.isString('42a718d6317dad85d3ea6ef6389d7db', { isMd5: true })); // false
contains
Check if string contains required chars (NOT only).
console.log(Datamatch.isString('dad33zzz', { contains: 'abcdefABCDEF123' })); // true
console.log(Datamatch.isString('zzz', { contains: 'abcdefABCDEF123' })); // false
containsOnly
Check if string contains required chars (only).
console.log(Datamatch.isString('dad33', { containsOnly: 'abcdefABCDEF123' })); // true
console.log(Datamatch.isString('dad33z', { containsOnly: 'abcdefABCDEF123' })); // false
isBase64
console.log(Datamatch.isString('SGVsbG8=', { isBase64: true })); // true
console.log(Datamatch.isString('SGVs', { isBase64: 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
isSha256
console.log(Datamatch.isString('0d2ddeef0b9f2fb02a3563aea38b059a0d2ddeef0b9f2fb02a3563aea38b059a', { isSha256: true })); // true
console.log(Datamatch.isString('0d2ddeef0b9f2fb02a3563aea38b059a0d2ddeef0b9f2fb02a3563aea38b059', { isSha256: true })); // false
License
Datamatch is released under the MIT License.