Simple and Fast Data Validation for Node.js
npm install datamatchMAJOR 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
- Example 1: Number validation
- Example 2: Number validation with options
- Example 3: Array validation
- Example 4: Array validation with options
- Example 5: Nested field validation
- Example 6: Nested field validation with options
- Example 7: AND logic
- Example 8: OR logic
- Default NodeJS TYPES
- 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
- OPTIONS
- contains
- containsOnly
- isBase64
- isDate
- isDomain
- isE164
- isEmail
- isFloat
- isHTTPSUrl
- isHTTPUrl
- isInt
- isIP
- isIPv4
- isIPv6
- isJSON
- isMd5
- isNumeric
- isSha256
- isUrl
- isWSSUrl
- isWSUrl
- length
- max
- maxLength
- min
- minLength
- values
- License
Install Datamatch using npm:
``bash`
npm i datamatch
Update Datamatch using npm:
`bash`
npm install datamatch@latest
`js`
import Datamatch from 'datamatch';`
ORjs`
const Datamatch = require('datamatch');
`js`
const penCount = 5;
console.log(Datamatch.isNumber(penCount)); // true
`js`
const penCount = 5;
console.log(Datamatch.isNumber(penCount, { max: 4 })); // false
`jsJohn
const firends = [ , Katrin, Tom ];`
console.log(Datamatch.isArray(firends)); // true
`jsJohn
const firends = [ , Katrin, Tom ];`
console.log(Datamatch.isArray(firends, { maxLength: 2 })); // false
`js
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.
`js
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:
`js
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:
`js
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.
js
console.log(Datamatch.isNumber(5, { min: 5 })); // true
console.log(Datamatch.isNumber(5, { min: 6 })); // falseconst ants = BigInt('1000000000000000000000000000000');
console.log(Datamatch.isBigInt(big, { min: '1000000000000000000000000000000' })); // true
console.log(Datamatch.isBigInt(big, { min: '1000000000000000000000000000001' })); // false
`max
`js
console.log(Datamatch.isNumber(5, { max: 5 })); // true
console.log(Datamatch.isNumber(5, { max: 4 })); // falseconst atoms = BigInt('3200000000000000000000000000000')
console.log(Datamatch.isBigInt(bigCount, { max: '3200000000000000000000000000000' })); // true
console.log(Datamatch.isBigInt(bigCount, { max: '3199999999999999999999999999999' })); // false
`length
`js
console.log(Datamatch.isNumber(9, { length: 1 })); // true
console.log(Datamatch.isNumber(9, { length: 2 })); // falseconsole.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
`js
console.log(Datamatch.isNumber(9, { minLength: 1 })); // true
console.log(Datamatch.isNumber(9, { minLength: 2 })); // falseconsole.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
`js
console.log(Datamatch.isNumber(9, { maxLength: 1 })); // true
console.log(Datamatch.isNumber(9, { maxLength: 0 })); // falseconsole.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
`js
console.log(Datamatch.isNumber(77, { values: [ 53, 77, 99 ] })); // true
console.log(Datamatch.isNumber(77, { values: [ 53, 99 ] })); // falseconsole.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.
`js
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
`js
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
`isE164
`js
console.log(Datamatch.isString('+74923341293', { isE164: true })); // true
console.log(Datamatch.isString('84923341293', { isE164: true })); // false
`isEmail
`js
console.log(Datamatch.isString('example@mysite.com', { isEmail: true })); // true
console.log(Datamatch.isString('example@.com', { isEmail: true })); // false
`isUrl
If string value is url (true), you can safely use '...new URL(value)...' without fear of errors.
`js
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.
`js
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.
`js
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.
`js
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.
`js
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
`js
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
`js
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
`js
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
`js
console.log(Datamatch.isString('{"login":"john"}', { isJSON: true })); // true
console.log(Datamatch.isString('{"login":"john', { isJSON: true })); // false
`isMd5
`js
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).
`js
console.log(Datamatch.isString('dad33zzz', { contains: 'abcdefABCDEF123' })); // true
console.log(Datamatch.isString('zzz', { contains: 'abcdefABCDEF123' })); // false
`containsOnly
Check if string contains required chars (only).
`js
console.log(Datamatch.isString('dad33', { containsOnly: 'abcdefABCDEF123' })); // true
console.log(Datamatch.isString('dad33z', { containsOnly: 'abcdefABCDEF123' })); // false
`isBase64
`js
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.
`js
console.log(Datamatch.isNumber(0.34, { isFloat: true })); // true
console.log(Datamatch.isNumber(2, { isFloat: true })); // falseconsole.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.
`js
console.log(Datamatch.isNumber(2, { isInt: true })); // true
console.log(Datamatch.isNumber(0.34, { isInt: true })); // falseconsole.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.
`js
console.log(Datamatch.isNumber(2, { isNumeric: true })); // true
console.log(Datamatch.isNumber(0.34, { isNumeric: true })); // true
console.log(Datamatch.isNumber(NaN, { isNumeric: true })); // falseconsole.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
`js
console.log(Datamatch.isString('0d2ddeef0b9f2fb02a3563aea38b059a0d2ddeef0b9f2fb02a3563aea38b059a', { isSha256: true })); // true
console.log(Datamatch.isString('0d2ddeef0b9f2fb02a3563aea38b059a0d2ddeef0b9f2fb02a3563aea38b059', { isSha256: true })); // false
``Datamatch is released under the MIT License.