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.
- 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
- OPTIONS
- License
Install Datamatch using npm:
npm i datamatch
Update Datamatch using npm:
npm install datamatch@latest
import Datamatch from 'datamatch';
OR
const Datamatch = require('datamatch');
const penCount = 5;
console.log(Datamatch.isNumber(penCount)); // true
const penCount = 5;
console.log(Datamatch.isNumber(penCount, { max: 4 })); // false
const firends = [ `John`, `Katrin`, `Tom` ];
console.log(Datamatch.isArray(firends)); // true
const firends = [ `John`, `Katrin`, `Tom` ];
console.log(Datamatch.isArray(firends, { maxLength: 2 })); // false
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.
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.
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'.
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.
Available options: no.
Available options: no.
Available options: no.
Available options:
min
max
length
minLength
maxLength
values
isFloat
isInt
isNumeric
Available options:
min
max
length
minLength
maxLength
values
Available options:
minLength
minLength
maxLength
values
isDate
isDomain
isUrl
isHTTPUrl
isHTTPSUrl
isWSUrl
isWSSUrl
isIP
isIPv4
isIPv6
isJSON
isBase64
isFloat
isInt
isNumeric
Available options:
length
minLength
maxLength
Available options: no.
Available options: no.
Available options: no.
Available options: no.
Available options: no.
Available options: no.
Available options: no.
Available options: no.
Available options: no.
Available options: no.
Available options: no.
Available options: no.
Available options: no.
Available options: no.
Available options: no.
Available options: no.
Available options: no.
Available options: no.
Available options: no.
Available options: no.
Available options: no.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
console.log(Datamatch.isString('{"login":"john"}', { isJSON: true })); // true
console.log(Datamatch.isString('{"login":"john', { isJSON: true })); // false
console.log(Datamatch.isString('SGVsbG8=', { isBase64: true })); // true
console.log(Datamatch.isString('SGVs', { isBase64: true })); // false
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
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
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
Datamatch is released under the MIT License.