@darkwolf/validator
Advanced tools
Comparing version 3.0.0 to 6.6.6
{ | ||
"name": "@darkwolf/validator", | ||
"version": "3.0.0", | ||
"description": "Validator Utility", | ||
"version": "6.6.6", | ||
"description": "Validator", | ||
"main": "src/index.js", | ||
@@ -14,5 +14,3 @@ "scripts": { | ||
"keywords": [ | ||
"validator", | ||
"utility", | ||
"utils" | ||
"validator" | ||
], | ||
@@ -26,4 +24,4 @@ "author": "Pavel Wolf", | ||
"dependencies": { | ||
"@darkwolf/code-error": "^2.1.0" | ||
"@darkwolf/code-error": "^6.6.6" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# Validator Utility | ||
# Validator | ||
## Install | ||
@@ -19,3 +19,3 @@ ```sh | ||
new Validator('69').isNumber() // throw error | ||
new Validator('69', {complete: true}).isNumber().isStrictEqual(69) // execute chain and throw error | ||
new Validator('69', {complete: true}).isNumber().isStrictEqual(69).throw() // execute chain and throw error | ||
``` | ||
@@ -26,3 +26,3 @@ ## Init | ||
### call(validator => validator) | ||
### validate(() => boolean, () => errorMessage) | ||
### validate(() => boolean, errorMessage || () => errorMessage) | ||
### throw(validator => e) | ||
@@ -58,4 +58,6 @@ ### isRequired() | ||
### isTypeError(value?) | ||
### isCodeError(value?) | ||
### isValidator(value?) | ||
### isCodeError(value?) | ||
### isValidatorError(value?) | ||
### isValidationError(value?) | ||
### isNaN(value?) | ||
@@ -62,0 +64,0 @@ ### isFinite(value?) |
@@ -0,1 +1,5 @@ | ||
const CodeError = require('@darkwolf/code-error') | ||
const ValidatorError = require('./ValidatorError') | ||
const ValidationError = require('./ValidationError') | ||
const errors = require('./errors') | ||
const constants = require('./constants') | ||
@@ -14,5 +18,6 @@ | ||
this.throwErrors = true | ||
this.error = new Error() | ||
this.error = new ValidationError('invalid-value') | ||
if (this._isPlainObject(options)) { | ||
if (this._isExists(options)) { | ||
if (!this._isPlainObject(options)) throw errors.INVALID_OPTIONS | ||
const { | ||
@@ -27,14 +32,19 @@ name, | ||
if (this._isExists(name)) { | ||
if (!this._isString(name)) throw errors.INVALID_NAME | ||
this.name = name | ||
} | ||
if (this._isExists(complete)) { | ||
if (!this._isBoolean(complete)) throw errors.INVALID_COMPLETE | ||
this.complete = complete | ||
} | ||
if (this._isExists(generateMessages)) { | ||
if (!this._isBoolean(generateMessages)) throw errors.INVALID_GENERATE_MESSAGES | ||
this.generateMessages = generateMessages | ||
} | ||
if (this._isExists(throwErrors)) { | ||
if (!this._isBoolean(throwErrors)) throw errors.INVALID_THROW_ERRORS | ||
this.throwErrors = throwErrors | ||
} | ||
if (this._isExists(customError)) { | ||
if (!this._isError(customError)) throw errors.INVALID_CUSTOM_ERROR | ||
this.error = customError | ||
@@ -162,2 +172,6 @@ } | ||
_isCodeError(value) { | ||
return this._isInstance(value, CodeError) | ||
} | ||
_isValidator(value) { | ||
@@ -167,7 +181,10 @@ return this._isInstance(value, Validator) | ||
_isCodeError(value) { | ||
const CodeError = require('@darkwolf/code-error') | ||
return this._isInstance(value, CodeError) | ||
_isValidatorError(value) { | ||
return this._isInstance(value, ValidatorError) | ||
} | ||
_isValidationError(value) { | ||
return this._isInstance(value, ValidationError) | ||
} | ||
_isNaN(value) { | ||
@@ -384,5 +401,4 @@ return this._isNumber(value) && isNaN(value) | ||
call(callback) { | ||
if (this._isFunction(callback)) { | ||
callback(this) | ||
} | ||
if (!this._isFunction(callback)) throw errors.INVALID_CALLBACK | ||
callback(this) | ||
return this | ||
@@ -392,2 +408,4 @@ } | ||
validate(callback, errorMessage, ...args) { | ||
if (!this._isFunction(callback)) throw errors.INVALID_CALLBACK | ||
if (!this._isString(errorMessage) && !this._isFunction(errorMessage)) throw errors.INVALID_ERROR_MESSAGE | ||
if (this.hasOwnProperty('value')) { | ||
@@ -401,3 +419,3 @@ if (!this.hasOwnProperty('optional')) { | ||
const message = this._isFunction(errorMessage) ? errorMessage(this.value, ...args) : errorMessage | ||
this.error.message = this.error.message.length ? `${this.error.message}\n${message}` : message | ||
this.error.message = this.error.message ? `${this.error.message}\n${message}` : message | ||
} | ||
@@ -414,10 +432,10 @@ if (!this.complete && this.throwErrors) throw this.error | ||
throw(...args) { | ||
if (args.length) { | ||
const [callback] = args | ||
if (!this._isFunction(callback)) throw errors.INVALID_CALLBACK | ||
} | ||
if (!this._isUndefined(this.isValid) && !this.isValid) { | ||
if (args.length) { | ||
const [callback] = args | ||
if (this._isFunction(callback)) { | ||
throw callback(this) | ||
} else { | ||
throw callback | ||
} | ||
throw callback(this) | ||
} else { | ||
@@ -543,2 +561,6 @@ throw this.error | ||
isCodeError(...args) { | ||
return this.validate((...args) => this._isCodeError(...args), () => `${this.name} must be an instance of CodeError`, ...args) | ||
} | ||
isValidator(...args) { | ||
@@ -548,6 +570,10 @@ 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), () => `${this.name} must be an instance of CodeError`, ...args) | ||
isValidatorError(...args) { | ||
return this.validate((...args) => this._isValidatorError(...args), () => `${this.name} must be an instance of ValidatorError`, ...args) | ||
} | ||
isValidationError(...args) { | ||
return this.validate((...args) => this._isValidationError(...args), () => `${this.name} must be an instance of ValidationError`, ...args) | ||
} | ||
isNaN(...args) { | ||
@@ -554,0 +580,0 @@ return this.validate((...args) => this._isNaN(...args), () => `${this.name} must be NaN`, ...args) |
30132
9
665
101
+ Added@darkwolf/code-error@6.6.6(transitive)
- Removed@darkwolf/code-error@2.1.0(transitive)
- Removed@darkwolf/validator@2.2.1(transitive)
Updated@darkwolf/code-error@^6.6.6