Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@darkwolf/validator

Package Overview
Dependencies
Maintainers
1
Versions
55
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@darkwolf/validator - npm Package Compare versions

Comparing version 2.1.5 to 2.1.6

449

index.js

@@ -1,7 +0,10 @@

const CodeError = require('@darkwolf/code-error')
class Validator {
constructor(...args) {
if (args.length) {
const [value] = args
class Validator {
constructor(value) {
this.value = value
this.isValid = undefined
this.value = value
this._setted = true
this.isValid = undefined
}
}

@@ -15,23 +18,57 @@

this.value = value
this._setted = true
return this
}
call(callback) {
call(validator) {
if (!this._isFunction(validator)) {
throw new TypeError('"validator" argument must be a callback function')
}
const value = callback(this)
return this._isUndefined(value) ? this : value
if (!this._isValidator(value)) {
throw new TypeError('"validator" argument must return an instance of Validator')
}
return this
}
throw(callback) {
if (!this.isUndefined(this.isValid) && !this.isValid) throw callback()
if (!this._isFunction(callback)) {
throw new TypeError('"callback" argument must be a callback function')
}
if (!this._isUndefined(this.isValid) && !this.isValid) throw callback()
return this
}
get CodeError() {
return require('@darkwolf/code-error')
}
_getTag(value) {
return Object.prototype.toString.call(value)
}
_isEqual(value1, value2) {
return value1 == value2
}
_isStrictEqual(value1, value2) {
return value1 === value2
}
_isType(value, type) {
return typeof value === type
return this._isEqual(typeof value, type)
}
_isObject(value) {
return this._isType(value, 'object')
_isTag(value, tag) {
return this._isEqual(this._getTag(value), `[object ${tag}]`)
}
_isInstance(value, constructor) {
try {
return value instanceof constructor
} catch (e) {
return false
}
}
_isUndefined(value) {

@@ -41,14 +78,22 @@ return this._isType(value, 'undefined')

_isBoolean(value) {
return this._isType(value, 'boolean')
_isNull(value) {
return this._isStrictEqual(value, null)
}
_isNumber(value) {
return this._isType(value, 'number')
_isNil(value) {
return this._isEqual(value, null)
}
_isString(value) {
return this._isType(value, 'string')
_isObject(value) {
return !this._isNull(value) && (this._isType(value, 'object') || this._isFunction(value))
}
_isObjectLike(value) {
return !this._isNull(value) && this._isType(value, 'object')
}
_isPlainObject(value) {
return this._isTag(value, 'Object')
}
_isFunction(value) {

@@ -58,14 +103,18 @@ return this._isType(value, 'function')

_isSymbol(value) {
return this._isType(value, 'symbol')
_isBoolean(value) {
return this._isType(value, 'boolean') || this._isTag(value, 'Boolean')
}
_isNull(value) {
return value === null
_isNumber(value) {
return this._isType(value, 'number') || this._isTag(value, 'Number')
}
_isInstance(value, instance) {
return value instanceof instance
_isString(value) {
return this._isType(value, 'string') || this._isTag(value, 'String')
}
_isSymbol(value) {
return this._isType(value, 'symbol') || this._isTag(value, 'Symbol')
}
_isArray(value) {

@@ -103,10 +152,2 @@ return Array.isArray(value)

_isTimestamp(value) {
return this._isInteger(value) && this._isRegex(value, /^[1-9]\d{11,12}$/)
}
_isUnixTimestamp(value) {
return this._isInteger(value) && this._isRegex(value, /^[1-9]\d{8,9}$/)
}
_isError(value) {

@@ -121,13 +162,5 @@ return this._isInstance(value, Error)

_isCodeError(value) {
return this._isInstance(value, CodeError)
return this._isInstance(value, this.CodeError)
}
_isObjectExact(value) {
return Object.prototype.toString.call(value) === '[object Object]'
}
_isExists(value) {
return !this._isUndefined(value) && !this._isNull(value)
}
_isNaN(value) {

@@ -153,3 +186,3 @@ return Number.isNaN(value)

_isFractional(value) {
_isDecimal(value) {
return this._isFinite(value) && !!(value % 1)

@@ -170,6 +203,2 @@ }

_isEqual(value1, value2) {
return value1 === value2
}
_isMoreEqual(value, number) {

@@ -204,24 +233,26 @@ return value >= number

_isTrue(value) {
return this._isEqual(value, true)
return this._isStrictEqual(value, true)
}
_isFalse(value) {
return this._isEqual(value, false)
return this._isStrictEqual(value, false)
}
_isRegex(value, regex) {
return regex.test(value)
if (this._isRegExp(regex)) return regex.test(value)
return false
}
_isRequired(value) {
return !this._isUndefined(value)
_isExists(value) {
return !this._isNil(value)
}
_isLength(value, ...args) {
if (this._isString(value) || this._isArray(value)) {
if (this._isString(value) || this._isArray(value) || this._isBuffer(value) || this._isArrayBuffer(value)) {
const length = value.length || value.byteLength
if (args.length) {
if (args.length > 1) return this._isRange(value.length, ...args)
return this._isEqual(value.length, ...args)
if (args.length > 1) return this._isRange(length, ...args)
return this._isStrictEqual(length, ...args)
}
return !!value.length
return !!length
}

@@ -231,16 +262,22 @@ return false

_isEmpty(value) {
if (this._isObject(value)) {
if (this._isArray(value)) return !!value.length
if (this._isSet(value) || this._isMap(value)) return !!value.size
return !!Object.keys(value).length
_isSize(value, ...args) {
if (this._isObjectLike(value)) {
const size = this._isSet(value) || this._isMap(value) ? value.size : Object.keys(value).length
if (args.length) {
if (args.length > 1) return this._isRange(size, ...args)
return this._isStrictEqual(size, ...args)
}
return !!size
}
if (this._isString(value)) return !!value.length
return false
}
_isEmpty(value) {
return !this._isLength(value) && !this._isSize(value)
}
_isHas(value, key) {
if (this._isObject(value)) {
if (this._isObjectLike(value)) {
if (this._isArray(value) || this._isSet(value)) {
return this._isRange(key, 0, value.length || value.size)
return this._isRange(key, 0, (value.length || value.size) - 1)
}

@@ -254,6 +291,9 @@ if (this._isMap(value)) return value.has(key)

_isIncludes(value1, value2) {
if (this._isArray(value1)) return value1.includes(value2)
if (this._isSet(value1)) return value1.has(value2)
if (this._isMap(value1)) {
return Array.from(value1).some(([key, value]) => value === value2)
if (this._isObjectLike(value1)) {
if (this._isArray(value1)) return value1.includes(value2)
if (this._isSet(value1)) return value1.has(value2)
if (this._isMap(value1)) {
return [...value1].some(([key, value]) => value === value2)
}
return Object.values(value1).includes(value2)
}

@@ -268,3 +308,3 @@ return false

_isUnique(value) {
if (this._isObject(value)) {
if (this._isObjectLike(value)) {
if (this._isArray(value)) {

@@ -275,3 +315,3 @@ return this._isEqual(value.length, [...new Set(value)].length)

if (this._isMap(value)) {
return this._isEqual(value.size, [...new Set(Array.from(value))].length)
return this._isEqual(value.size, [...new Set([...value])].length)
}

@@ -281,5 +321,16 @@ const values = Object.values(value)

}
if (this._isString(value)) {
return this._isEqual(value.length, [...new Set(value)].length)
}
return false
}
_isTimestamp(value) {
return this._isInteger(value) && this._isRegex(value, /^[1-9]\d{11,12}$/)
}
_isUnixTimestamp(value) {
return this._isInteger(value) && this._isRegex(value, /^[1-9]\d{8,9}$/)
}
_isUUID(value) {

@@ -305,300 +356,266 @@ return this._isRegex(value, /^[a-f\d]{8}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{12}$/i)

_valid(callback) {
if (!this._isOptional) {
if (this._isUndefined(this.isValid) || this.isValid) {
this.isValid = !!callback()
_validate(method, args) {
if (this._setted) {
if (!this._isOptional) {
if (this._isUndefined(this.isValid) || this.isValid) {
this.isValid = this[method](this.value, ...args)
}
}
return this
}
return this
return this[method](...args)
}
isRequired() {
if (this._setted) {
this.isValid = !this._isUndefined(this.value)
return this
}
}
isOptional() {
if (this._setted) {
this._isOptional = this._isUndefined(this.value)
return this
}
}
isEqual(...args) {
return this._validate('_isEqual', args)
}
isStrictEqual(...args) {
return this._validate('_isStrictEqual', args)
}
isType(...args) {
if (args.length > 1) return this._isType(...args)
return this._valid(() => this._isType(this.value, ...args))
return this._validate('_isType', args)
}
isObject(...args) {
if (args.length) return this._isObject(...args)
return this._valid(() => this._isObject(this.value))
isTag(...args) {
return this._validate('_isTag', args)
}
isInstance(...args) {
return this._validate('_isInstance', args)
}
isUndefined(...args) {
if (args.length) return this._isUndefined(...args)
return this._valid(() => this._isUndefined(this.value))
return this._validate('_isUndefined', args)
}
isBoolean(...args) {
if (args.length) return this._isBoolean(...args)
return this._valid(() => this._isBoolean(this.value))
isNull(...args) {
return this._validate('_isNull', args)
}
isNumber(...args) {
if (args.length) return this._isNumber(...args)
return this._valid(() => this._isNumber(this.value))
isNil(...args) {
return this._validate('_isNil', args)
}
isString(...args) {
if (args.length) return this._isString(...args)
return this._valid(() => this._isString(this.value))
isObject(...args) {
return this._validate('_isObject', args)
}
isObjectLike(...args) {
return this._validate('_isObjectLike', args)
}
isPlainObject(...args) {
return this._validate('_isPlainObject', args)
}
isFunction(...args) {
if (args.length) return this._isFunction(...args)
return this._valid(() => this._isFunction(this.value))
return this._validate('_isFunction', args)
}
isSymbol(...args) {
if (args.length) return this._isSymbol(...args)
return this._valid(() => this._isSymbol(this.value))
isBoolean(...args) {
return this._validate('_isBoolean', args)
}
isNull(...args) {
if (args.length) return this._isNull(...args)
return this._valid(() => this._isNull(this.value))
isNumber(...args) {
return this._validate('_isNumber', args)
}
isInstance(...args) {
if (args.length > 1) return this._isInstance(...args)
return this._valid(() => this._isInstance(this.value, ...args))
isString(...args) {
return this._validate('_isString', args)
}
isArray(...args) {
if (args.length) return this._isArray(...args)
return this._valid(() => this._isArray(this.value))
return this._validate('_isArray', args)
}
isBuffer(...args) {
if (args.length) return this._isBuffer(...args)
return this._valid(() => this._isBuffer(this.value))
return this._validate('_isBuffer', args)
}
isArrayBuffer(...args) {
if (args.length) return this._isArrayBuffer(...args)
return this._valid(() => this._isArrayBuffer(this.value))
return this._validate('_isArrayBuffer', args)
}
isRegExp(...args) {
if (args.length) return this._isRegExp(...args)
return this._valid(() => this._isRegExp(this.value))
return this._validate('_isRegExp', args)
}
isSet(...args) {
if (args.length) return this._isSet(...args)
return this._valid(() => this._isSet(this.value))
return this._validate('_isSet', args)
}
isMap(...args) {
if (args.length) return this._isMap(...args)
return this._valid(() => this._isMap(this.value))
return this._validate('_isMap', args)
}
isPromise(...args) {
if (args.length) return this._isPromise(...args)
return this._valid(() => this._isPromise(this.value))
return this._validate('_isPromise', args)
}
isDate(...args) {
if (args.length) return this._isDate(...args)
return this._valid(() => this._isDate(this.value))
return this._validate('_isDate', args)
}
isTimestamp(...args) {
if (args.length) return this._isTimestamp(...args)
return this._valid(() => this._isTimestamp(this.value))
}
isUnixTimestamp(...args) {
if (args.length) return this._isUnixTimestamp(...args)
return this._valid(() => this._isUnixTimestamp(this.value))
}
isError(...args) {
if (args.length) return this._isError(...args)
return this._valid(() => this._isError(this.value))
return this._validate('_isError', args)
}
isTypeError(...args) {
if (args.length) return this._isTypeError(...args)
return this._valid(() => this._isTypeError(this.value))
return this._validate('_isTypeError', args)
}
isCodeError(...args) {
if (args.length) return this._isCodeError(...args)
return this._valid(() => this._isCodeError(this.value))
return this._validate('_isCodeError', args)
}
isObjectExact(...args) {
if (args.length) return this._isObjectExact(...args)
return this._valid(() => this._isObjectExact(this.value))
}
isExists(...args) {
if (args.length) return this._isExists(...args)
return this._valid(() => this._isExists(this.value))
}
isNaN(...args) {
if (args.length) return this._isNaN(...args)
return this._valid(() => this._isNaN(this.value))
return this._validate('_isNaN', args)
}
isFinite(...args) {
if (args.length) return this._isFinite(...args)
return this._valid(() => this._isFinite(this.value))
return this._validate('_isFinite', args)
}
isInteger(...args) {
if (args.length) return this._isInteger(...args)
return this._valid(() => this._isInteger(this.value))
return this._validate('_isInteger', args)
}
isSafeInteger(...args) {
if (args.length) return this._isSafeInteger(...args)
return this._valid(() => this._isSafeInteger(this.value))
return this._validate('_isSafeInteger', args)
}
isFloat(...args) {
if (args.length) return this._isFloat(...args)
return this._valid(() => this._isFloat(this.value))
return this._validate('_isFloat', args)
}
isFractional(...args) {
if (args.length) return this._isFractional(...args)
return this._valid(() => this._isFractional(this.value))
isDecimal(...args) {
return this._validate('_isDecimal', args)
}
isInfinity(...args) {
if (args.length) return this._isInfinity(...args)
return this._valid(() => this._isInfinity(this.value))
return this._validate('_isInfinity', args)
}
isMore(...args) {
if (args.length > 1) return this._isMore(...args)
return this._valid(() => this._isMore(this.value, ...args))
return this._validate('_isMore', args)
}
isLess(...args) {
if (args.length > 1) return this._isLess(...args)
return this._valid(() => this._isLess(this.value, ...args))
return this._validate('_isLess', args)
}
isEqual(...args) {
if (args.length > 1) return this._isEqual(...args)
return this._valid(() => this._isEqual(this.value, ...args))
}
isMoreEqual(...args) {
if (args.length > 1) return this._isMoreEqual(...args)
return this._valid(() => this._isMoreEqual(this.value, ...args))
return this._validate('_isMoreEqual', args)
}
isLessEqual(...args) {
if (args.length > 1) return this._isLessEqual(...args)
return this._valid(() => this._isLessEqual(this.value, ...args))
return this._validate('_isLessEqual', args)
}
isRange(...args) {
if (args.length > 2) return this._isRange(...args)
return this._valid(() => this._isRange(this.value, ...args))
return this._validate('_isRange', args)
}
isPositive(...args) {
if (args.length) return this._isPositive(...args)
return this._valid(() => this._isPositive(this.value))
return this._validate('_isPositive', args)
}
isNegative(...args) {
if (args.length) return this._isNegative(...args)
return this._valid(() => this._isNegative(this.value))
return this._validate('_isNegative', args)
}
isNonNegative(...args) {
if (args.length) return this._isNonNegative(...args)
return this._valid(() => this._isNonNegative(this.value))
return this._validate('_isNonNegative', args)
}
isNonPositive(...args) {
if (args.length) return this._isNonPositive(...args)
return this._valid(() => this._isNonPositive(this.value))
return this._validate('_isNonPositive', args)
}
isTrue(...args) {
if (args.length) return this._isTrue(...args)
return this._valid(() => this._isTrue(this.value))
return this._validate('_isTrue', args)
}
isFalse(...args) {
if (args.length) return this._isFalse(...args)
return this._valid(() => this._isFalse(this.value))
return this._validate('_isFalse', args)
}
isRegex(...args) {
if (args.length) return this._isRegex(...args)
return this._valid(() => this._isRegex(this.value, ...args))
return this._validate('_isRegex', args)
}
isRequired(...args) {
if (args.length) return this._isRequired(...args)
return this._valid(() => this._isRequired(this.value))
isExists(...args) {
return this._validate('_isExists', args)
}
isOptional(...args) {
if (args.length) return this._isUndefined(...args)
this._isOptional = this.isUndefined(this.value)
return this
isLength(...args) {
return this._validate('_isLength', args)
}
isLength(...args) {
if (args.length && this._isUndefined(this.value)) return this._isLength(...args)
return this._valid(() => this._isLength(this.value, ...args))
isSize(...args) {
return this._validate('_isSize', args)
}
isEmpty(...args) {
if (args.length) return this._isEmpty(...args)
return this._valid(() => this._isEmpty(this.value))
return this._validate('_isEmpty', args)
}
isHas(...args) {
if (args.length > 1) return this._isHas(...args)
return this._valid(() => this._isHas(this.value, ...args))
return this._validate('_isHas', args)
}
isIncludes(...args) {
if (args.length > 1) return this._isIncludes(...args)
return this._valid(() => this._isIncludes(this.value, ...args))
return this._validate('_isIncludes', args)
}
isIncluded(...args) {
if (args.length > 1) return this._isIncluded(...args)
return this._valid(() => this._isIncluded(this.value, ...args))
return this._validate('_isIncluded', args)
}
isUnique(...args) {
if (args.length) return this._isUnique(...args)
return this._valid(() => this._isUnique(this.value))
return this._validate('_isUnique', args)
}
isTimestamp(...args) {
return this._validate('_isTimestamp', args)
}
isUnixTimestamp(...args) {
return this._validate('_isUnixTimestamp', args)
}
isUUID(...args) {
if (args.length) return this._isUUID(...args)
return this._valid(() => this._isUUID(this.value))
return this._validate('_isUUID', args)
}
isIPv4(...args) {
if (args.length) return this._isIPv4(...args)
return this._valid(() => this._isIPv4(this.value))
return this._validate('_isIPv4', args)
}
isUrl(...args) {
if (args.length) return this._isUrl(...args)
return this._valid(() => this._isUrl(this.value))
return this._validate('_isUrl', args)
}
isEmail(...args) {
if (args.length) return this._isEmail(...args)
return this._valid(() => this._isEmail(this.value))
return this._validate('_isEmail', args)
}
isPhoneNumber(...args) {
if (args.length) return this._isPhoneNumber(...args)
return this._valid(() => this._isPhoneNumber(this.value))
return this._validate('_isPhoneNumber', args)
}

@@ -605,0 +622,0 @@ }

{
"name": "@darkwolf/validator",
"version": "2.1.5",
"version": "2.1.6",
"description": "Validator Utility",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -29,12 +29,19 @@ # Validator Utility

### throw(() => e)
### isRequired()
### isOptional()
### isEqual(value1?, value2)
### isStrictEqual(value1?, value2)
### isType(value?, type)
### isTag(value?, tag)
### isInstance(value?, constructor)
### isUndefined(value?)
### isNull(value?)
### isNil(value?)
### isObject(value?)
### isUndefined(value?)
### isObjectLike(value?)
### isPlainObject(value?)
### isFunction(value?)
### isBoolean(value?)
### isNumber(value?)
### isString(value?)
### isFunction(value?)
### isSymbol(value?)
### isNull(value?)
### isInstance(value?, instance)
### isArray(value?)

@@ -48,11 +55,5 @@ ### isBuffer(value?)

### isDate(value?)
### isTimestamp(value?)
### isUnixTimestamp(value?)
### isError(value?)
### isTypeError(value?)
### isCodeError(value?)
### isObjectExact(value?)
### isTrue(value?)
### isFalse(value?)
### isExists(value?)
### isNaN(value?)

@@ -63,7 +64,6 @@ ### isFinite(value?)

### isFloat(value?)
### isFractional(value?)
### isDecimal(value?)
### isInfinity(value?)
### isMore(value?, number)
### isLess(value?, number)
### isEqual(value?, number)
### isMoreEqual(value?, number)

@@ -76,11 +76,15 @@ ### isLessEqual(value?, number)

### isNonPositive(value?)
### isTrue(value?)
### isFalse(value?)
### isRegex(value?, regex)
### isRequired(value?)
### isOptional(value?)
### isLength(value?, length? || ...[min, max])
### isExists(value?)
### isLength(value?, number || ...[min, max])
### isSize(value?, number || ...[min, max])
### isEmpty(value?)
### isHas(value?, key)
### isIncludes(value?, value)
### isIncluded(value?, array)
### isIncludes(value1?, value2)
### isIncluded(value1?, value2)
### isUnique(value?)
### isTimestamp(value?)
### isUnixTimestamp(value?)
### isUUID(value?)

@@ -87,0 +91,0 @@ ### isIPv4(value?)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc