fenextjs-validator
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -1,2 +0,19 @@ | ||
export declare class FenextjsValidator { | ||
export declare class FenextjsValidatorClass<T = any> { | ||
#private; | ||
isEqual(d: T): this; | ||
isRequired(): this; | ||
isBoolean(): this; | ||
isNumber(): this; | ||
isString(): this; | ||
isDate(): this; | ||
isObject(obj: { | ||
[id: string]: FenextjsValidatorClass; | ||
}): this; | ||
isArray(item: FenextjsValidatorClass): this; | ||
isMin(min: number | Date): this; | ||
isMinOrEqual(min: number | Date): this; | ||
isMax(max: number | Date): this; | ||
isMaxOrEqual(max: number | Date): this; | ||
onValidate(d: T): unknown; | ||
} | ||
export declare const FenextjsValidator: () => FenextjsValidatorClass<any>; |
237
cjs/index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.FenextjsValidator = void 0; | ||
class FenextjsValidator { | ||
exports.FenextjsValidator = exports.FenextjsValidatorClass = void 0; | ||
const fenextjs_error_1 = require("fenextjs-error"); | ||
class FenextjsValidatorClass { | ||
#data; | ||
#equal = false; | ||
#equalValue = undefined; | ||
#required = false; | ||
#boolean = false; | ||
#number = false; | ||
#string = false; | ||
#date = false; | ||
#object = false; | ||
#objectValue = undefined; | ||
#array = false; | ||
#arrayValue = undefined; | ||
#min = false; | ||
#minOrEqual = false; | ||
#minValue = undefined; | ||
#max = false; | ||
#maxOrEqual = false; | ||
#maxValue = undefined; | ||
isEqual(d) { | ||
this.#equal = true; | ||
this.#equalValue = d; | ||
return this; | ||
} | ||
#onEqual() { | ||
if (!this.#equal) { | ||
return; | ||
} | ||
if (this.#equalValue !== this.#data) { | ||
throw new fenextjs_error_1.ErrorInputInvalid(); | ||
} | ||
return this; | ||
} | ||
isRequired() { | ||
this.#required = true; | ||
return this; | ||
} | ||
#onRequered() { | ||
if (!this.#required) { | ||
return; | ||
} | ||
if (this.#data === null || | ||
this.#data == undefined || | ||
this.#data === "") { | ||
throw new fenextjs_error_1.ErrorInputRequired(); | ||
} | ||
} | ||
isBoolean() { | ||
this.#boolean = true; | ||
return this; | ||
} | ||
#onBoolean() { | ||
if (!this.#boolean) { | ||
return; | ||
} | ||
if (typeof this.#data != "boolean") { | ||
throw new fenextjs_error_1.ErrorInputInvalid(); | ||
} | ||
} | ||
isNumber() { | ||
this.#number = true; | ||
return this; | ||
} | ||
#onNumber() { | ||
if (!this.#number) { | ||
return; | ||
} | ||
if (typeof this.#data != "number") { | ||
throw new fenextjs_error_1.ErrorInputInvalid(); | ||
} | ||
} | ||
isString() { | ||
this.#string = true; | ||
return this; | ||
} | ||
#onString() { | ||
if (!this.#string) { | ||
return; | ||
} | ||
if (typeof this.#data != "string") { | ||
throw new fenextjs_error_1.ErrorInputInvalid(); | ||
} | ||
} | ||
isDate() { | ||
this.#date = true; | ||
return this; | ||
} | ||
#onDate() { | ||
if (!this.#date) { | ||
return; | ||
} | ||
if (!(this.#data instanceof Date)) { | ||
throw new fenextjs_error_1.ErrorInputInvalid(); | ||
} | ||
} | ||
isObject(obj) { | ||
this.#object = true; | ||
this.#objectValue = obj; | ||
return this; | ||
} | ||
#onObject() { | ||
if (!this.#object || !this.#objectValue) { | ||
return; | ||
} | ||
if (typeof this.#data != "object") { | ||
throw new fenextjs_error_1.ErrorInputInvalid(); | ||
} | ||
const keys = Object.keys(this.#objectValue); | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i]; | ||
const validator = this.#objectValue[key]; | ||
const r = validator.onValidate(this.#data?.[key]); | ||
if (r !== true) { | ||
throw r; | ||
} | ||
} | ||
} | ||
isArray(item) { | ||
this.#array = true; | ||
this.#arrayValue = item; | ||
return this; | ||
} | ||
#onArray() { | ||
if (!this.#array || !this.#arrayValue) { | ||
return; | ||
} | ||
if (!Array.isArray(this.#data)) { | ||
throw new fenextjs_error_1.ErrorInputInvalid(); | ||
} | ||
for (let i = 0; i < this.#data.length; i++) { | ||
const item = this.#data[i]; | ||
const validator = this.#arrayValue; | ||
const r = validator.onValidate(item); | ||
if (r !== true) { | ||
throw r; | ||
} | ||
} | ||
} | ||
isMin(min) { | ||
this.#min = true; | ||
this.#minValue = min; | ||
return this; | ||
} | ||
isMinOrEqual(min) { | ||
this.#minOrEqual = true; | ||
this.#minValue = min; | ||
return this; | ||
} | ||
#onMin() { | ||
let minValidate = undefined; | ||
if (Array.isArray(this.#data)) { | ||
minValidate = this.#data.length; | ||
} | ||
if (typeof this.#data == "number") { | ||
minValidate = this.#data; | ||
} | ||
if (typeof this.#data == "string") { | ||
minValidate = this.#data.length; | ||
} | ||
if (this.#data instanceof Date) { | ||
minValidate = this.#data.getTime(); | ||
} | ||
let nMinValue = this.#minValue; | ||
if (nMinValue instanceof Date) { | ||
nMinValue = nMinValue.getTime(); | ||
} | ||
if (this.#min && | ||
!(minValidate && nMinValue && minValidate > nMinValue)) { | ||
throw new fenextjs_error_1.ErrorInputInvalid(); | ||
} | ||
if (this.#minOrEqual && | ||
!(minValidate && nMinValue && minValidate >= nMinValue)) { | ||
throw new fenextjs_error_1.ErrorInputInvalid(); | ||
} | ||
} | ||
isMax(max) { | ||
this.#max = true; | ||
this.#maxValue = max; | ||
return this; | ||
} | ||
isMaxOrEqual(max) { | ||
this.#maxOrEqual = true; | ||
this.#maxValue = max; | ||
return this; | ||
} | ||
#onMax() { | ||
let maxValidate = undefined; | ||
if (Array.isArray(this.#data)) { | ||
maxValidate = this.#data.length; | ||
} | ||
if (typeof this.#data == "number") { | ||
maxValidate = this.#data; | ||
} | ||
if (typeof this.#data == "string") { | ||
maxValidate = this.#data.length; | ||
} | ||
if (this.#data instanceof Date) { | ||
maxValidate = this.#data.getTime(); | ||
} | ||
let nMaxValue = this.#maxValue; | ||
if (nMaxValue instanceof Date) { | ||
nMaxValue = nMaxValue.getTime(); | ||
} | ||
if (this.#max && | ||
!(maxValidate && nMaxValue && maxValidate < nMaxValue)) { | ||
throw new fenextjs_error_1.ErrorInputInvalid(); | ||
} | ||
if (this.#maxOrEqual && | ||
!(maxValidate && nMaxValue && maxValidate <= nMaxValue)) { | ||
throw new fenextjs_error_1.ErrorInputInvalid(); | ||
} | ||
} | ||
onValidate(d) { | ||
try { | ||
this.#data = d; | ||
this.#onEqual(); | ||
this.#onRequered(); | ||
this.#onBoolean(); | ||
this.#onNumber(); | ||
this.#onString(); | ||
this.#onDate(); | ||
this.#onObject(); | ||
this.#onArray(); | ||
this.#onMin(); | ||
this.#onMax(); | ||
return true; | ||
} | ||
catch (error) { | ||
return error; | ||
} | ||
} | ||
} | ||
exports.FenextjsValidatorClass = FenextjsValidatorClass; | ||
const FenextjsValidator = () => new FenextjsValidatorClass(); | ||
exports.FenextjsValidator = FenextjsValidator; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "fenextjs-validator", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "", | ||
@@ -9,2 +9,3 @@ "main": "./cjs/index.js", | ||
"scripts": { | ||
"test": "npm run build:cjs && node ./cjs/test.js", | ||
"build": "npm run build:esm && npm run build:cjs", | ||
@@ -39,4 +40,4 @@ "build:esm": "echo 'esm'", | ||
"dependencies": { | ||
"fenextjs-interface": "^1.0.23", | ||
"fenextjs-error": "^1.0.3" | ||
"fenextjs-error": "^1.0.3", | ||
"fenextjs-interface": "^1.0.23" | ||
}, | ||
@@ -43,0 +44,0 @@ "files": [ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
18931
8
297