Comparing version 1.0.5 to 2.0.0
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -39,2 +48,5 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
static addGlobalValidators(validators) { | ||
if (typeof validators === "object") { | ||
validators = Object.values(validators); | ||
} | ||
if (Array.isArray(validators)) { | ||
@@ -46,3 +58,3 @@ for (const value of validators) { | ||
else { | ||
throw new TypeError("addGlobalValidators argument must be an array"); | ||
throw new TypeError("addGlobalValidators argument must be an array or an object"); | ||
} | ||
@@ -76,2 +88,5 @@ } | ||
addValidators(validators) { | ||
if (typeof validators === "object") { | ||
validators = Object.values(validators); | ||
} | ||
if (Array.isArray(validators)) { | ||
@@ -83,3 +98,3 @@ for (const value of validators) { | ||
else { | ||
throw new TypeError("addValidators argument must be an array"); | ||
throw new TypeError("addValidators argument must be an array or an object"); | ||
} | ||
@@ -139,3 +154,3 @@ return this; | ||
*/ | ||
let validated = { ...object }; | ||
let validated = Object.assign({}, object); | ||
/** | ||
@@ -202,3 +217,3 @@ * Get Keys to be validated | ||
*/ | ||
ruleData = { ...internalWildcardRules, ...ruleData }; | ||
ruleData = Object.assign(Object.assign({}, internalWildcardRules), ruleData); | ||
/** | ||
@@ -257,4 +272,4 @@ * Loop through ruleData to check if validators defined exists | ||
*/ | ||
return { | ||
error: { | ||
return [ | ||
{ | ||
key: rule, | ||
@@ -265,8 +280,9 @@ type: 'internal', | ||
data: e.stack | ||
} | ||
}; | ||
}, | ||
{} | ||
]; | ||
} | ||
if (validationResult instanceof AbolishError_1.default) { | ||
return { | ||
error: { | ||
return [ | ||
{ | ||
key: rule, | ||
@@ -277,4 +293,5 @@ type: 'validator', | ||
data: validationResult.data | ||
} | ||
}; | ||
}, | ||
{} | ||
]; | ||
} | ||
@@ -300,4 +317,4 @@ else if (validationResult === false) { | ||
// Return Error using the ValidationResult format | ||
return { | ||
error: { | ||
return [ | ||
{ | ||
key: rule, | ||
@@ -308,4 +325,5 @@ type: 'validator', | ||
data: null | ||
} | ||
}; | ||
}, | ||
{} | ||
]; | ||
} | ||
@@ -322,6 +340,6 @@ } | ||
validated = Functions_1.Pick(validated, keysToBeValidated); | ||
return { | ||
error: false, | ||
return [ | ||
false, | ||
validated | ||
}; | ||
]; | ||
} | ||
@@ -352,3 +370,3 @@ /** | ||
*/ | ||
return new Promise(async (resolve) => { | ||
return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () { | ||
/** | ||
@@ -369,3 +387,3 @@ * Loop through jobs and run their validators | ||
*/ | ||
validationResult = await validator.validator(objectValue, validatorOption, { | ||
validationResult = yield validator.validator(objectValue, validatorOption, { | ||
error: (message, data) => new AbolishError_1.default(message, data), | ||
@@ -380,4 +398,3 @@ modifier: new ObjectModifier_1.default(validated, rule) | ||
*/ | ||
return resolve({ | ||
error: { | ||
return resolve([{ | ||
key: rule, | ||
@@ -388,8 +405,6 @@ type: 'internal', | ||
data: e.stack | ||
} | ||
}); | ||
}, {}]); | ||
} | ||
if (validationResult instanceof AbolishError_1.default) { | ||
return resolve({ | ||
error: { | ||
return resolve([{ | ||
key: rule, | ||
@@ -400,4 +415,3 @@ type: 'validator', | ||
data: validationResult.data | ||
} | ||
}); | ||
}, {}]); | ||
} | ||
@@ -421,4 +435,3 @@ else if (validationResult === false) { | ||
// Return Error using the ValidationResult format | ||
return resolve({ | ||
error: { | ||
return resolve([{ | ||
key: rule, | ||
@@ -429,13 +442,9 @@ type: 'validator', | ||
data: null | ||
} | ||
}); | ||
}, {}]); | ||
} | ||
} | ||
return resolve({ | ||
error: false, | ||
validated: Functions_1.Pick(validated, keysToBeValidated) | ||
}); | ||
}); | ||
return resolve([false, Functions_1.Pick(validated, keysToBeValidated)]); | ||
})); | ||
} | ||
} | ||
module.exports = Abolish; |
@@ -60,3 +60,3 @@ "use strict"; | ||
rule = StringToRules_1.default(rule); | ||
generatedRule = { ...generatedRule, ...rule }; | ||
generatedRule = Object.assign(Object.assign({}, generatedRule), rule); | ||
} | ||
@@ -63,0 +63,0 @@ return generatedRule; |
@@ -117,3 +117,3 @@ "use strict"; | ||
$inline: { | ||
name: '$custom', | ||
name: '$inline', | ||
error: ':param failed inline validation.', | ||
@@ -120,0 +120,0 @@ validator: (v, o, helpers) => { |
"use strict"; | ||
module.exports = [ | ||
require('./any') | ||
]; | ||
const any = require("./any"); | ||
module.exports = { | ||
any | ||
}; |
"use strict"; | ||
module.exports = [ | ||
require('./alphaNumeric'), | ||
require('./boolean'), | ||
require('./date'), | ||
require('./email'), | ||
require('./ipAddress'), | ||
require('./json'), | ||
require('./jsonDecode'), | ||
require('./number'), | ||
require('./regex'), | ||
require('./url') | ||
]; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.url = exports.regex = exports.number = exports.jsonDecode = exports.json = exports.ipAddress = exports.email = exports.date = exports.boolean = exports.alphaNumeric = void 0; | ||
const alphaNumeric = require("./alphaNumeric"); | ||
exports.alphaNumeric = alphaNumeric; | ||
const boolean = require("./boolean"); | ||
exports.boolean = boolean; | ||
const date = require("./date"); | ||
exports.date = date; | ||
const email = require("./email"); | ||
exports.email = email; | ||
const ipAddress = require("./ipAddress"); | ||
exports.ipAddress = ipAddress; | ||
const json = require("./json"); | ||
exports.json = json; | ||
const jsonDecode = require("./jsonDecode"); | ||
exports.jsonDecode = jsonDecode; | ||
const number = require("./number"); | ||
exports.number = number; | ||
const regex = require("./regex"); | ||
exports.regex = regex; | ||
const url = require("./url"); | ||
exports.url = url; |
{ | ||
"name": "abolish", | ||
"version": "1.0.5", | ||
"version": "2.0.0", | ||
"description": "A javascript object validator.", | ||
@@ -16,12 +16,12 @@ "main": "js/index.js", | ||
"dependencies": { | ||
"@types/lodash.has": "^4.5.6", | ||
"@types/lodash.unset": "^4.5.6", | ||
"lodash.has": "^4.5.2", | ||
"lodash.unset": "^4.5.2", | ||
"@types/lodash.has": "^4.5.6", | ||
"@types/lodash.unset": "^4.5.6" | ||
"lodash.unset": "^4.5.2" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^14.14.9", | ||
"axios": "^0.21.0", | ||
"ts-node-dev": "^1.0.0", | ||
"typescript": "^4.1.2" | ||
"@types/node": "^14.14.16", | ||
"axios": "^0.21.1", | ||
"ts-node-dev": "^1.1.1", | ||
"typescript": "^4.1.3" | ||
}, | ||
@@ -28,0 +28,0 @@ "keywords": [ |
@@ -40,3 +40,7 @@ import {AbolishValidator, ValidationResult} from "./Types" | ||
*/ | ||
static addGlobalValidators(validators: AbolishValidator[]) { | ||
static addGlobalValidators(validators: AbolishValidator[] | Record<string, AbolishValidator>) { | ||
if (typeof validators === "object") { | ||
validators = Object.values(validators); | ||
} | ||
if (Array.isArray(validators)) { | ||
@@ -47,3 +51,3 @@ for (const value of validators) { | ||
} else { | ||
throw new TypeError("addGlobalValidators argument must be an array") | ||
throw new TypeError("addGlobalValidators argument must be an array or an object") | ||
} | ||
@@ -81,3 +85,7 @@ } | ||
*/ | ||
addValidators(validators: AbolishValidator[]): this { | ||
addValidators(validators: AbolishValidator[] | Record<string, AbolishValidator>): this { | ||
if (typeof validators === "object") { | ||
validators = Object.values(validators); | ||
} | ||
if (Array.isArray(validators)) { | ||
@@ -88,3 +96,3 @@ for (const value of validators) { | ||
} else { | ||
throw new TypeError("addValidators argument must be an array") | ||
throw new TypeError("addValidators argument must be an array or an object") | ||
} | ||
@@ -102,3 +110,3 @@ | ||
*/ | ||
static validate(object: Record<string, any>, rules: Record<string, any>): ValidationResult { | ||
static validate<R = Record<string, any>>(object: Record<string, any>, rules: Record<keyof R | string, any>): ValidationResult<R> { | ||
return (new this).validate(object, rules); | ||
@@ -115,3 +123,3 @@ } | ||
*/ | ||
static validateAsync(object: Record<string, any>, rules: Record<string, any>): Promise<ValidationResult> { | ||
static validateAsync<R = Record<string, any>>(object: Record<string, any>, rules: Record<keyof R | string, any>): Promise<ValidationResult<R>> { | ||
return (new this).validateAsync(object, rules) | ||
@@ -128,3 +136,3 @@ } | ||
*/ | ||
validate(object: Record<string, any>, rules: Record<string, any>, isAsync = false): ValidationResult { | ||
validate<R = Record<string, any>>(object: Record<string, any>, rules: Record<keyof R | string, any>, isAsync = false): ValidationResult<R> { | ||
@@ -293,4 +301,4 @@ let asyncData = { | ||
*/ | ||
return { | ||
error: { | ||
return [ | ||
{ | ||
key: rule, | ||
@@ -301,4 +309,4 @@ type: 'internal', | ||
data: e.stack | ||
} | ||
} | ||
}, {} as R | ||
] | ||
} | ||
@@ -308,4 +316,4 @@ | ||
if (validationResult instanceof AbolishError) { | ||
return { | ||
error: { | ||
return [ | ||
{ | ||
key: rule, | ||
@@ -316,4 +324,4 @@ type: 'validator', | ||
data: validationResult.data | ||
} | ||
} | ||
}, {} as R | ||
] | ||
} else if (validationResult === false) { | ||
@@ -340,4 +348,4 @@ /** | ||
// Return Error using the ValidationResult format | ||
return { | ||
error: { | ||
return [ | ||
{ | ||
key: rule, | ||
@@ -348,4 +356,4 @@ type: 'validator', | ||
data: null | ||
} | ||
} | ||
}, {} as R | ||
] | ||
} | ||
@@ -365,6 +373,6 @@ } | ||
return { | ||
error: false, | ||
validated | ||
} | ||
return [ | ||
false, | ||
validated as R | ||
] | ||
} | ||
@@ -380,3 +388,3 @@ | ||
*/ | ||
validateAsync(object: Record<string, any>, rules: Record<string, any>): Promise<ValidationResult> { | ||
validateAsync<R = Record<string, any>>(object: Record<string, any>, rules: Record<keyof R | string, any>): Promise<ValidationResult<R>> { | ||
/** | ||
@@ -429,11 +437,9 @@ * Get asyncData | ||
*/ | ||
return resolve({ | ||
error: { | ||
key: rule, | ||
type: 'internal', | ||
validator: validatorName, | ||
message: e.message, | ||
data: e.stack | ||
} | ||
}) | ||
return resolve([{ | ||
key: rule, | ||
type: 'internal', | ||
validator: validatorName, | ||
message: e.message, | ||
data: e.stack | ||
}, {}]) | ||
} | ||
@@ -443,11 +449,9 @@ | ||
if (validationResult instanceof AbolishError) { | ||
return resolve({ | ||
error: { | ||
key: rule, | ||
type: 'validator', | ||
validator: validatorName, | ||
message: $error ? $error : validationResult.message, | ||
data: validationResult.data | ||
} | ||
}) | ||
return resolve([{ | ||
key: rule, | ||
type: 'validator', | ||
validator: validatorName, | ||
message: $error ? $error : validationResult.message, | ||
data: validationResult.data | ||
}, {}]) | ||
} else if (validationResult === false) { | ||
@@ -472,18 +476,13 @@ /** | ||
// Return Error using the ValidationResult format | ||
return resolve({ | ||
error: { | ||
key: rule, | ||
type: 'validator', | ||
validator: validatorName, | ||
message, | ||
data: null | ||
} | ||
}) | ||
return resolve([{ | ||
key: rule, | ||
type: 'validator', | ||
validator: validatorName, | ||
message, | ||
data: null | ||
}, {}]) | ||
} | ||
} | ||
return resolve({ | ||
error: false, | ||
validated: Pick(validated, keysToBeValidated) | ||
}) | ||
return resolve([false, Pick(validated, keysToBeValidated)]) | ||
}); | ||
@@ -490,0 +489,0 @@ } |
@@ -149,3 +149,3 @@ /** | ||
$inline: { | ||
name: '$custom', | ||
name: '$inline', | ||
error: ':param failed inline validation.', | ||
@@ -152,0 +152,0 @@ validator: (v: any, o: AbolishInlineValidator, helpers) => { |
@@ -23,6 +23,6 @@ import AbolishError from "./AbolishError"; | ||
*/ | ||
export type ValidationResult = { | ||
export type ValidationResult<T=any> = [ | ||
error: ValidationError | false, | ||
validated?: any | ||
} | ||
validated: T | ||
] | ||
@@ -29,0 +29,0 @@ export type AbolishValidatorFunction = ( |
{ | ||
"compilerOptions": { | ||
/* Visit https://aka.ms/tsconfig.json to read more about this file */ | ||
/* Basic Options */ | ||
// "incremental": true, /* Enable incremental compilation */ | ||
"target": "ES2019", | ||
"target": "es6", | ||
/* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */ | ||
"module": "commonjs", | ||
/* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ | ||
"lib": [ | ||
"dom", | ||
"es2019" | ||
], | ||
/* Specify library files to be included in the compilation. */ | ||
// "lib": [], /* Specify library files to be included in the compilation. */ | ||
// "allowJs": true, /* Allow javascript files to be compiled. */ | ||
@@ -20,3 +18,2 @@ // "checkJs": true, /* Report errors in .js files. */ | ||
"declarationDir": "types", | ||
/* Declaration file directory*/ | ||
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ | ||
@@ -52,6 +49,6 @@ // "sourceMap": true, /* Generates corresponding '.map' file. */ | ||
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ | ||
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ | ||
/* Module Resolution Options */ | ||
"moduleResolution": "node", | ||
/* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ | ||
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ | ||
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ | ||
@@ -79,2 +76,4 @@ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ | ||
/* Advanced Options */ | ||
"skipLibCheck": false, | ||
/* Skip type checking of declaration files. */ | ||
"forceConsistentCasingInFileNames": true | ||
@@ -81,0 +80,0 @@ /* Disallow inconsistently-cased references to the same file. */ |
@@ -19,3 +19,3 @@ import { AbolishValidator, ValidationResult } from "./Types"; | ||
*/ | ||
static addGlobalValidators(validators: AbolishValidator[]): void; | ||
static addGlobalValidators(validators: AbolishValidator[] | Record<string, AbolishValidator>): void; | ||
/** | ||
@@ -34,3 +34,3 @@ * addValidator | ||
*/ | ||
addValidators(validators: AbolishValidator[]): this; | ||
addValidators(validators: AbolishValidator[] | Record<string, AbolishValidator>): this; | ||
/** | ||
@@ -43,3 +43,3 @@ * Validate | ||
*/ | ||
static validate(object: Record<string, any>, rules: Record<string, any>): ValidationResult; | ||
static validate<R = Record<string, any>>(object: Record<string, any>, rules: Record<keyof R | string, any>): ValidationResult<R>; | ||
/** | ||
@@ -53,3 +53,3 @@ * Validate Async | ||
*/ | ||
static validateAsync(object: Record<string, any>, rules: Record<string, any>): Promise<ValidationResult>; | ||
static validateAsync<R = Record<string, any>>(object: Record<string, any>, rules: Record<keyof R | string, any>): Promise<ValidationResult<R>>; | ||
/** | ||
@@ -63,3 +63,3 @@ * Validate | ||
*/ | ||
validate(object: Record<string, any>, rules: Record<string, any>, isAsync?: boolean): ValidationResult; | ||
validate<R = Record<string, any>>(object: Record<string, any>, rules: Record<keyof R | string, any>, isAsync?: boolean): ValidationResult<R>; | ||
/** | ||
@@ -73,4 +73,4 @@ * Validate Async | ||
*/ | ||
validateAsync(object: Record<string, any>, rules: Record<string, any>): Promise<ValidationResult>; | ||
validateAsync<R = Record<string, any>>(object: Record<string, any>, rules: Record<keyof R | string, any>): Promise<ValidationResult<R>>; | ||
} | ||
export = Abolish; |
@@ -20,6 +20,6 @@ import AbolishError from "./AbolishError"; | ||
*/ | ||
export declare type ValidationResult = { | ||
error: ValidationError | false; | ||
validated?: any; | ||
}; | ||
export declare type ValidationResult<T = any> = [ | ||
error: ValidationError | false, | ||
validated: T | ||
]; | ||
export declare type AbolishValidatorFunction = (value: any, option: any, helpers: { | ||
@@ -26,0 +26,0 @@ error: (message: string, data?: any) => AbolishError; |
@@ -1,2 +0,4 @@ | ||
declare const _default: any[]; | ||
declare const _default: { | ||
any: import("../../src/Types").AbolishValidator; | ||
}; | ||
export = _default; |
@@ -1,2 +0,11 @@ | ||
declare const _default: any[]; | ||
export = _default; | ||
import alphaNumeric = require("./alphaNumeric"); | ||
import boolean = require("./boolean"); | ||
import date = require("./date"); | ||
import email = require("./email"); | ||
import ipAddress = require("./ipAddress"); | ||
import json = require("./json"); | ||
import jsonDecode = require("./jsonDecode"); | ||
import number = require("./number"); | ||
import regex = require("./regex"); | ||
import url = require("./url"); | ||
export { alphaNumeric, boolean, date, email, ipAddress, json, jsonDecode, number, regex, url }; |
@@ -1,3 +0,5 @@ | ||
export = [ | ||
require('./any') | ||
] | ||
import any = require("./any"); | ||
export = { | ||
any | ||
} |
@@ -1,12 +0,23 @@ | ||
export = [ | ||
require('./alphaNumeric'), | ||
require('./boolean'), | ||
require('./date'), | ||
require('./email'), | ||
require('./ipAddress'), | ||
require('./json'), | ||
require('./jsonDecode'), | ||
require('./number'), | ||
require('./regex'), | ||
require('./url') | ||
] | ||
import alphaNumeric = require("./alphaNumeric"); | ||
import boolean = require("./boolean"); | ||
import date = require("./date"); | ||
import email = require("./email"); | ||
import ipAddress = require("./ipAddress"); | ||
import json = require("./json"); | ||
import jsonDecode = require("./jsonDecode"); | ||
import number = require("./number"); | ||
import regex = require("./regex"); | ||
import url = require("./url"); | ||
export { | ||
alphaNumeric, | ||
boolean, | ||
date, | ||
email, | ||
ipAddress, | ||
json, | ||
jsonDecode, | ||
number, | ||
regex, | ||
url | ||
} |
90769
67
2430