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.2.1 to 3.0.0

2

package.json
{
"name": "@darkwolf/validator",
"version": "2.2.1",
"version": "3.0.0",
"description": "Validator Utility",

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

@@ -16,12 +16,13 @@ # Validator Utility

new Validator(69).isString().isValid // false
new Validator(69, {throwErrors: false}).isString().isValid // false
new Validator(69).isNumber().isRange(0, 100).isValid // true
new Validator('69')
.isNumber()
.throw(() => new TypeError('Value must be a number')) // throw error
new Validator('69').isNumber() // throw error
new Validator('69', {complete: true}).isNumber().isStrictEqual(69) // execute chain and throw error
```
## Init
### new Validator(value?)
### new Validator(value?, options[name, complete, generateMessages, throwErrors, customError])
## Methods
### throw(() => e)
### call(validator => validator)
### validate(() => boolean, () => errorMessage)
### throw(validator => e)
### isRequired()

@@ -28,0 +29,0 @@ ### isOptional()

@@ -6,5 +6,37 @@ const constants = require('./constants')

if (args.length) {
const [value] = args
const [value, options] = args
this.value = value
this.isValid = undefined
this.name = 'Value'
this.complete = false
this.generateMessages = true
this.throwErrors = true
this.error = new Error()
if (this._isPlainObject(options)) {
const {
name,
complete,
throwErrors,
generateMessages,
customError
} = options
if (this._isExists(name)) {
this.name = name
}
if (this._isExists(complete)) {
this.complete = complete
}
if (this._isExists(generateMessages)) {
this.generateMessages = generateMessages
}
if (this._isExists(throwErrors)) {
this.throwErrors = throwErrors
}
if (this._isExists(customError)) {
this.error = customError
}
}
}

@@ -348,7 +380,22 @@ }

_validate(callback, ...args) {
call(callback) {
if (this._isFunction(callback)) {
callback(this)
}
return this
}
validate(callback, errorMessage, ...args) {
if (this.hasOwnProperty('value')) {
if (!this.hasOwnProperty('optional')) {
if (!this.hasOwnProperty('isValid') || this.isValid) {
this.isValid = !!callback(this.value, ...args)
if (this._isUndefined(this.isValid) || this.isValid || this.complete) {
const isValid = !!callback(this.value, ...args)
this.isValid = this.complete ? (this._isUndefined(this.isValid) || this.isValid) && isValid : isValid
if (!isValid) {
if (this.generateMessages) {
const message = this._isFunction(errorMessage) ? errorMessage(this.value, ...args) : errorMessage
this.error.message = this.error.message.length ? `${this.error.message}\n${message}` : message
}
if (!this.complete && this.throwErrors) throw this.error
}
}

@@ -361,10 +408,20 @@ }

throw(callback) {
if (this.hasOwnProperty('isValid') && !this.isValid) throw callback()
throw(...args) {
if (!this._isUndefined(this.isValid) && !this.isValid) {
if (args.length) {
const [callback] = args
if (this._isFunction(callback)) {
throw callback(this)
} else {
throw callback
}
} else {
throw this.error
}
}
return this
}
isRequired() {
this.isValid = this.hasOwnProperty('value')
return this
isRequired(...args) {
return this.validate(value => !this._isUndefined(value), () => `${this.name} is required`, ...args)
}

@@ -377,276 +434,298 @@

is(callback) {
return this._validate(() => this._isFunction(callback) && callback(this.value))
is(...args) {
return this.validate((value, callback) => this._isFunction(callback) ? callback(value) : callback, () => `${this.name} is invalid`, ...args)
}
isEqual(...args) {
return this._validate((...args) => this._isEqual(...args), ...args)
return this.validate((...args) => this._isEqual(...args), (value1, value2) => `${this.name} must be equal to ${this._isString(value2) ? `"${value2}"` : value2}`, ...args)
}
isStrictEqual(...args) {
return this._validate((...args) => this._isStrictEqual(...args), ...args)
return this.validate((...args) => this._isStrictEqual(...args), (value1, value2) => `${this.name} must be strictly equal to ${this._isString(value2) ? `"${value2}"` : value2}`, ...args)
}
isType(...args) {
return this._validate((...args) => this._isType(...args), ...args)
return this.validate((...args) => this._isType(...args), (value, type) => `${this.name} must be ${type}`, ...args)
}
isTag(...args) {
return this._validate((...args) => this._isTag(...args), ...args)
return this.validate((...args) => this._isTag(...args), (value, tag) => `${this.name} must be ${tag}`, ...args)
}
isInstance(...args) {
return this._validate((...args) => this._isInstance(...args), ...args)
return this.validate((...args) => this._isInstance(...args), (value, constructor) => `${this.name} must be an instance of ${constructor.name}`, ...args)
}
isUndefined(...args) {
return this._validate((...args) => this._isUndefined(...args), ...args)
return this.validate((...args) => this._isUndefined(...args), () => `${this.name} must be undefined`, ...args)
}
isNull(...args) {
return this._validate((...args) => this._isNull(...args), ...args)
return this.validate((...args) => this._isNull(...args), () => `${this.name} must be null`, ...args)
}
isNil(...args) {
return this._validate((...args) => this._isNil(...args), ...args)
return this.validate((...args) => this._isNil(...args), () => `${this.name} must be nil`, ...args)
}
isObject(...args) {
return this._validate((...args) => this._isObject(...args), ...args)
return this.validate((...args) => this._isObject(...args), () => `${this.name} must be an object`, ...args)
}
isObjectLike(...args) {
return this._validate((...args) => this._isObjectLike(...args), ...args)
return this.validate((...args) => this._isObjectLike(...args), () => `${this.name} must be an object-like`, ...args)
}
isPlainObject(...args) {
return this._validate((...args) => this._isPlainObject(...args), ...args)
return this.validate((...args) => this._isPlainObject(...args), () => `${this.name} must be a plain object`, ...args)
}
isFunction(...args) {
return this._validate((...args) => this._isFunction(...args), ...args)
return this.validate((...args) => this._isFunction(...args), () => `${this.name} must be a function`, ...args)
}
isBoolean(...args) {
return this._validate((...args) => this._isBoolean(...args), ...args)
return this.validate((...args) => this._isBoolean(...args), () => `${this.name} must be boolean`, ...args)
}
isNumber(...args) {
return this._validate((...args) => this._isNumber(...args), ...args)
return this.validate((...args) => this._isNumber(...args), () => `${this.name} must be a number`, ...args)
}
isString(...args) {
return this._validate((...args) => this._isString(...args), ...args)
return this.validate((...args) => this._isString(...args), () => `${this.name} must be a string`, ...args)
}
isArray(...args) {
return this._validate((...args) => this._isArray(...args), ...args)
return this.validate((...args) => this._isArray(...args), () => `${this.name} must be an array`, ...args)
}
isBuffer(...args) {
return this._validate((...args) => this._isBuffer(...args), ...args)
return this.validate((...args) => this._isBuffer(...args), () => `${this.name} must be a buffer`, ...args)
}
isArrayBuffer(...args) {
return this._validate((...args) => this._isArrayBuffer(...args), ...args)
return this.validate((...args) => this._isArrayBuffer(...args), () => `${this.name} must be an instance of ArrayBuffer`, ...args)
}
isRegExp(...args) {
return this._validate((...args) => this._isRegExp(...args), ...args)
return this.validate((...args) => this._isRegExp(...args), () => `${this.name} must be an instance of RegExp`, ...args)
}
isSet(...args) {
return this._validate((...args) => this._isSet(...args), ...args)
return this.validate((...args) => this._isSet(...args), () => `${this.name} must be an instance of Set`, ...args)
}
isMap(...args) {
return this._validate((...args) => this._isMap(...args), ...args)
return this.validate((...args) => this._isMap(...args), () => `${this.name} must be an instance of Map`, ...args)
}
isPromise(...args) {
return this._validate((...args) => this._isPromise(...args), ...args)
return this.validate((...args) => this._isPromise(...args), () => `${this.name} must be a promise`, ...args)
}
isDate(...args) {
return this._validate((...args) => this._isDate(...args), ...args)
return this.validate((...args) => this._isDate(...args), () => `${this.name} must be an instance of Date`, ...args)
}
isError(...args) {
return this._validate((...args) => this._isError(...args), ...args)
return this.validate((...args) => this._isError(...args), () => `${this.name} must be an instance of Error`, ...args)
}
isTypeError(...args) {
return this._validate((...args) => this._isTypeError(...args), ...args)
return this.validate((...args) => this._isTypeError(...args), () => `${this.name} must be an instance of TypeError`, ...args)
}
isValidator(...args) {
return this._validate((...args) => this._isValidator(...args), ...args)
return this.validate((...args) => this._isValidator(...args), () => `${this.name} must be an instance of Validator`, ...args)
}
isCodeError(...args) {
return this._validate((...args) => this._isCodeError(...args), ...args)
return this.validate((...args) => this._isCodeError(...args), () => `${this.name} must be an instance of CodeError`, ...args)
}
isNaN(...args) {
return this._validate((...args) => this._isNaN(...args), ...args)
return this.validate((...args) => this._isNaN(...args), () => `${this.name} must be NaN`, ...args)
}
isFinite(...args) {
return this._validate((...args) => this._isFinite(...args), ...args)
return this.validate((...args) => this._isFinite(...args), () => `${this.name} must be a finite number`, ...args)
}
isInteger(...args) {
return this._validate((...args) => this._isInteger(...args), ...args)
return this.validate((...args) => this._isInteger(...args), () => `${this.name} must be an integer`, ...args)
}
isSafeInteger(...args) {
return this._validate((...args) => this._isSafeInteger(...args), ...args)
return this.validate((...args) => this._isSafeInteger(...args), () => `${this.name} must be a safe integer`, ...args)
}
isFloat(...args) {
return this._validate((...args) => this._isFloat(...args), ...args)
return this.validate((...args) => this._isFloat(...args), () => `${this.name} must be a float number`, ...args)
}
isDecimal(...args) {
return this._validate((...args) => this._isDecimal(...args), ...args)
return this.validate((...args) => this._isDecimal(...args), () => `${this.name} must be a decimal number`, ...args)
}
isInfinity(...args) {
return this._validate((...args) => this._isInfinity(...args), ...args)
return this.validate((...args) => this._isInfinity(...args), () => `${this.name} must be infinity`, ...args)
}
isMore(...args) {
return this._validate((...args) => this._isMore(...args), ...args)
return this.validate((...args) => this._isMore(...args), (value, number) => `${this.name} must be more than ${number}`, ...args)
}
isLess(...args) {
return this._validate((...args) => this._isLess(...args), ...args)
return this.validate((...args) => this._isLess(...args), (value, number) => `${this.name} must be less than ${number}`, ...args)
}
isMoreEqual(...args) {
return this._validate((...args) => this._isMoreEqual(...args), ...args)
return this.validate((...args) => this._isMoreEqual(...args), (value, number) => `${this.name} must be more than or equal to ${number}`, ...args)
}
isLessEqual(...args) {
return this._validate((...args) => this._isLessEqual(...args), ...args)
return this.validate((...args) => this._isLessEqual(...args), (value, number) => `${this.name} must be less than or equal to ${number}`, ...args)
}
isRange(...args) {
return this._validate((...args) => this._isRange(...args), ...args)
return this.validate((...args) => this._isRange(...args), (value, min, max) => `${this.name} must be between ${min} and ${max}`, ...args)
}
isPositive(...args) {
return this._validate((...args) => this._isPositive(...args), ...args)
return this.validate((...args) => this._isPositive(...args), () => `${this.name} must be a positive number`, ...args)
}
isNegative(...args) {
return this._validate((...args) => this._isNegative(...args), ...args)
return this.validate((...args) => this._isNegative(...args), () => `${this.name} must be a negative number`, ...args)
}
isNonNegative(...args) {
return this._validate((...args) => this._isNonNegative(...args), ...args)
return this.validate((...args) => this._isNonNegative(...args), () => `${this.name} must be a non-negative number`, ...args)
}
isNonPositive(...args) {
return this._validate((...args) => this._isNonPositive(...args), ...args)
return this.validate((...args) => this._isNonPositive(...args), () => `${this.name} must be a non-positive number`, ...args)
}
isEven(...args) {
return this._validate((...args) => this._isEven(...args), ...args)
return this.validate((...args) => this._isEven(...args), () => `${this.name} must be an even number`, ...args)
}
isOdd(...args) {
return this._validate((...args) => this._isOdd(...args), ...args)
return this.validate((...args) => this._isOdd(...args), () => `${this.name} must be an odd number`, ...args)
}
isTrue(...args) {
return this._validate((...args) => this._isTrue(...args), ...args)
return this.validate((...args) => this._isTrue(...args), () => `${this.name} must be true`, ...args)
}
isFalse(...args) {
return this._validate((...args) => this._isFalse(...args), ...args)
return this.validate((...args) => this._isFalse(...args), () => `${this.name} must be false`, ...args)
}
isRegex(...args) {
return this._validate((...args) => this._isRegex(...args), ...args)
return this.validate((...args) => this._isRegex(...args), () => `${this.name} has the wrong format`, ...args)
}
isExists(...args) {
return this._validate((...args) => this._isExists(...args), ...args)
return this.validate((...args) => this._isExists(...args), () => `${this.name} doesn\'t exist`, ...args)
}
isLength(...args) {
return this._validate((...args) => this._isLength(...args), ...args)
return this.validate((...args) => this._isLength(...args), (value, ...args) => {
if (args.length) {
if (args.length > 1) {
const [min, max] = args
return `${this.name} must have a length of ${min} to ${max}`
} else {
const [number] = args
return `${this.name} must have a length of ${number}`
}
}
return `${this.name} must have a length`
}, ...args)
}
isSize(...args) {
return this._validate((...args) => this._isSize(...args), ...args)
return this.validate((...args) => this._isSize(...args), (value, ...args) => {
if (args.length) {
if (args.length > 1) {
const [min, max] = args
return `${this.name} must have a size of ${min} to ${max}`
} else {
const [number] = args
return `${this.name} must have a size of ${number}`
}
}
return `${this.name} must have a size`
}, ...args)
}
isEmpty(...args) {
return this._validate((...args) => this._isEmpty(...args), ...args)
return this.validate((...args) => this._isEmpty(...args), () => `${this.name} must be empty`, ...args)
}
isHas(...args) {
return this._validate((...args) => this._isHas(...args), ...args)
return this.validate((...args) => this._isHas(...args), (value, key) => `${this.name} must have the key "${key}"`, ...args)
}
isIncludes(...args) {
return this._validate((...args) => this._isIncludes(...args), ...args)
return this.validate((...args) => this._isIncludes(...args), (value1, value2) => `${this.name} must include ${this._isString(value2) ? `"${value2}"` : value2}`, ...args)
}
isIncluded(...args) {
return this._validate((...args) => this._isIncluded(...args), ...args)
return this.validate((...args) => this._isIncluded(...args), (value1, value2) => `${this.name} must be ${value2.map(o => this._isString(o) ? `"${o}"` : o).join(', ')}`, ...args)
}
isUnique(...args) {
return this._validate((...args) => this._isUnique(...args), ...args)
return this.validate((...args) => this._isUnique(...args), () => `${this.name} must be unique`, ...args)
}
isTimestamp(...args) {
return this._validate((...args) => this._isTimestamp(...args), ...args)
return this.validate((...args) => this._isTimestamp(...args), () => `${this.name} must be an integer timestamp`, ...args)
}
isUnixTimestamp(...args) {
return this._validate((...args) => this._isUnixTimestamp(...args), ...args)
return this.validate((...args) => this._isUnixTimestamp(...args), () => `${this.name} must be an integer Unix timestamp`, ...args)
}
isUUID(...args) {
return this._validate((...args) => this._isUUID(...args), ...args)
return this.validate((...args) => this._isUUID(...args), () => `${this.name} must be UUID`, ...args)
}
isIPv4(...args) {
return this._validate((...args) => this._isIPv4(...args), ...args)
return this.validate((...args) => this._isIPv4(...args), () => `${this.name} must be IPv4`, ...args)
}
isUrl(...args) {
return this._validate((...args) => this._isUrl(...args), ...args)
return this.validate((...args) => this._isUrl(...args), () => `${this.name} must be URL`, ...args)
}
isEmail(...args) {
return this._validate((...args) => this._isEmail(...args), ...args)
return this.validate((...args) => this._isEmail(...args), () => `${this.name} must be email`, ...args)
}
isPhoneNumber(...args) {
return this._validate((...args) => this._isPhoneNumber(...args), ...args)
return this.validate((...args) => this._isPhoneNumber(...args), () => `${this.name} must be a phone number`, ...args)
}
isASCII(...args) {
return this._validate((...args) => this._isASCII(...args), ...args)
return this.validate((...args) => this._isASCII(...args), () => `${this.name} must be ASCII`, ...args)
}
isBase64(...args) {
return this._validate((...args) => this._isBase64(...args), ...args)
return this.validate((...args) => this._isBase64(...args), () => `${this.name} must be Base64`, ...args)
}
isBase64Url(...args) {
return this._validate((...args) => this._isBase64Url(...args), ...args)
return this.validate((...args) => this._isBase64Url(...args), () => `${this.name} must be Base64Url`, ...args)
}
isBase58(...args) {
return this._validate((...args) => this._isBase58(...args), ...args)
return this.validate((...args) => this._isBase58(...args), () => `${this.name} must be Base58`, ...args)
}
isJWT(...args) {
return this._validate((...args) => this._isJWT(...args), ...args)
return this.validate((...args) => this._isJWT(...args), () => `${this.name} must be JWT`, ...args)
}

@@ -653,0 +732,0 @@ }

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