Comparing version 4.2.1 to 4.3.0
{ | ||
"name": "abolish", | ||
"version": "4.2.1", | ||
"version": "4.3.0", | ||
"description": "A javascript object validator.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,2 @@ | ||
import { AbolishValidator, ValidationResult } from "./Types"; | ||
import { AbolishRule, AbolishValidator, ValidationError, ValidationResult } from "./Types"; | ||
import type Joi from "joi"; | ||
@@ -78,4 +78,57 @@ /** | ||
validateAsync<R = Record<string, any>>(object: Record<string, any>, rules: Record<keyof R | string, any>): Promise<ValidationResult<R>>; | ||
/** | ||
* Enables the use of $joi validator | ||
* @param joi | ||
*/ | ||
static useJoi(joi?: Joi.Root): typeof Abolish; | ||
/** | ||
* check a variable does not throw error | ||
* @param variable | ||
* @param rules | ||
*/ | ||
check<V = any>(variable: V, rules: AbolishRule): [ValidationError | false, V]; | ||
/** | ||
* Static Check | ||
* @param variable | ||
* @param rules | ||
*/ | ||
static check<V = any>(variable: V, rules: AbolishRule): [ValidationError | false, V]; | ||
/** | ||
* Checks a variable Asynchronously | ||
* @param variable | ||
* @param rules | ||
*/ | ||
checkAsync<V = any>(variable: V, rules: AbolishRule): Promise<[ValidationError | false, V]>; | ||
/** | ||
* Static Check Async | ||
* @param variable | ||
* @param rules | ||
*/ | ||
static checkAsync<V = any>(variable: V, rules: AbolishRule): Promise<[ValidationError | false, V]>; | ||
/** | ||
* Validates a variable | ||
* @param variable | ||
* @param rules | ||
*/ | ||
attempt<V = any>(variable: V, rules: Record<string, any> | string): V; | ||
/** | ||
* Static Attempt | ||
* @param variable | ||
* @param rules | ||
* @param abolish | ||
*/ | ||
static attempt<V = any>(variable: V, rules: Record<string, any> | string, abolish?: typeof Abolish): V; | ||
/** | ||
* Validates a variable Asynchronously, Throws error | ||
* @param variable | ||
* @param rules | ||
*/ | ||
attemptAsync<V = any>(variable: V, rules: Record<string, any> | string | string[]): Promise<V>; | ||
/** | ||
* Validates a variable Asynchronously, Throws error | ||
* @param variable | ||
* @param rules | ||
*/ | ||
static attemptAsync<V = any>(variable: V, rules: AbolishRule): Promise<V>; | ||
} | ||
export = Abolish; |
@@ -159,3 +159,3 @@ "use strict"; | ||
/** | ||
* Get Keys to be validated | ||
* abolish_Get Keys to be validated | ||
*/ | ||
@@ -256,3 +256,3 @@ let keysToBeValidated = Object.keys(rules); | ||
*/ | ||
const objectValue = Functions_1.Get(validated, rule); | ||
const objectValue = Functions_1.abolish_Get(validated, rule); | ||
/** | ||
@@ -329,3 +329,3 @@ * If is async push needed data to asyncData | ||
*/ | ||
let message = ($error ? $error : validator.error).replace(":param", $name ? $name : Functions_1.StartCase(rule, this)); | ||
let message = ($error ? $error : validator.error).replace(":param", $name ? $name : Functions_1.abolish_StartCase(rule, this)); | ||
if (optionIsStringable) | ||
@@ -354,4 +354,4 @@ message = message.replace(":option", validatorOption); | ||
} | ||
// Pick only keys in rules | ||
validated = Functions_1.Pick(validated, keysToBeValidated); | ||
// abolish_Pick only keys in rules | ||
validated = Functions_1.abolish_Pick(validated, keysToBeValidated); | ||
return [false, validated]; | ||
@@ -369,3 +369,3 @@ } | ||
/** | ||
* Get asyncData | ||
* abolish_Get asyncData | ||
*/ | ||
@@ -389,3 +389,3 @@ const asyncData = this.validate(object, rules, true); | ||
*/ | ||
const objectValue = Functions_1.Get(validated, rule); | ||
const objectValue = Functions_1.abolish_Get(validated, rule); | ||
let validationResult = false; | ||
@@ -443,3 +443,3 @@ try { | ||
*/ | ||
let message = ($error ? $error : validator.error).replace(":param", $name ? $name : Functions_1.StartCase(rule, this)); | ||
let message = ($error ? $error : validator.error).replace(":param", $name ? $name : Functions_1.abolish_StartCase(rule, this)); | ||
if (optionIsStringable) | ||
@@ -460,5 +460,9 @@ message = message.replace(":option", validatorOption); | ||
} | ||
return resolve([false, Functions_1.Pick(validated, keysToBeValidated)]); | ||
return resolve([false, Functions_1.abolish_Pick(validated, keysToBeValidated)]); | ||
}); | ||
} | ||
/** | ||
* Enables the use of $joi validator | ||
* @param joi | ||
*/ | ||
static useJoi(joi) { | ||
@@ -496,3 +500,3 @@ if (!joi) { | ||
/** | ||
* Set Value for abolish | ||
* abolish_Set Value for abolish | ||
*/ | ||
@@ -504,3 +508,76 @@ modifier.setThis(validated); | ||
} | ||
/** | ||
* check a variable does not throw error | ||
* @param variable | ||
* @param rules | ||
*/ | ||
check(variable, rules) { | ||
const [e, v] = this.validate({ variable }, { variable: rules }); | ||
return [e, v === null || v === void 0 ? void 0 : v.variable]; | ||
} | ||
/** | ||
* Static Check | ||
* @param variable | ||
* @param rules | ||
*/ | ||
static check(variable, rules) { | ||
return new this().check(variable, rules); | ||
} | ||
/** | ||
* Checks a variable Asynchronously | ||
* @param variable | ||
* @param rules | ||
*/ | ||
async checkAsync(variable, rules) { | ||
const [e, v] = await this.validateAsync({ variable }, { variable: rules }); | ||
return [e, v === null || v === void 0 ? void 0 : v.variable]; | ||
} | ||
/** | ||
* Static Check Async | ||
* @param variable | ||
* @param rules | ||
*/ | ||
static checkAsync(variable, rules) { | ||
return new this().checkAsync(variable, rules); | ||
} | ||
/** | ||
* Validates a variable | ||
* @param variable | ||
* @param rules | ||
*/ | ||
attempt(variable, rules) { | ||
const [e, v] = this.validate({ variable }, { variable: rules }); | ||
if (e) | ||
throw new Error(e.message); | ||
return v.variable; | ||
} | ||
/** | ||
* Static Attempt | ||
* @param variable | ||
* @param rules | ||
* @param abolish | ||
*/ | ||
static attempt(variable, rules, abolish) { | ||
return new this().attempt(variable, rules); | ||
} | ||
/** | ||
* Validates a variable Asynchronously, Throws error | ||
* @param variable | ||
* @param rules | ||
*/ | ||
async attemptAsync(variable, rules) { | ||
const [e, v] = await this.validateAsync({ variable }, { variable: rules }); | ||
if (e) | ||
throw new Error(e.message); | ||
return v.variable; | ||
} | ||
/** | ||
* Validates a variable Asynchronously, Throws error | ||
* @param variable | ||
* @param rules | ||
*/ | ||
static async attemptAsync(variable, rules) { | ||
return new this().attemptAsync(variable, rules); | ||
} | ||
} | ||
module.exports = Abolish; |
import Abolish from "./Abolish"; | ||
import { AbolishInlineValidator, ValidationError } from "./Types"; | ||
import { AbolishInlineValidator } from "./Types"; | ||
/** | ||
@@ -8,5 +8,5 @@ * Change to string to upperFirst | ||
*/ | ||
export declare function UpperFirst(str: string): string; | ||
export declare function abolish_UpperFirst(str: string): string; | ||
/** | ||
* StartCase | ||
* abolish_StartCase | ||
* @param str | ||
@@ -16,3 +16,3 @@ * @param abolishInstance | ||
*/ | ||
export declare function StartCase(str: string, abolishInstance?: Abolish): string; | ||
export declare function abolish_StartCase(str: string, abolishInstance?: Abolish): string; | ||
/** | ||
@@ -26,39 +26,10 @@ * Converts an array of rules (string | object)[] to one object rule | ||
* @param rules | ||
* @param options | ||
* @constructor | ||
*/ | ||
export declare function Rule(rules: any[]): any; | ||
export declare function Pick(object: any, keys: string[]): any; | ||
export declare function Set(object: any, path: any, value: any): any; | ||
export declare function Get(obj: any, path: string, defaultValue?: any): any; | ||
export declare function abolish_Pick(object: any, keys: string[]): any; | ||
export declare function abolish_Set(object: any, path: any, value: any): any; | ||
export declare function abolish_Get(obj: any, path: string, defaultValue?: any): any; | ||
export declare function ParseRules<Rules = Record<string, any>>(rules: Record<keyof Rules | string, any>): Rules; | ||
/** | ||
* Validates a variable | ||
* @param variable | ||
* @param rules | ||
* @param abolish | ||
*/ | ||
export declare const attempt: <V = any>(variable: V, rules: Record<string, any> | string, abolish?: typeof Abolish | undefined) => V; | ||
/** | ||
* Validates a variable Asynchronously, Throws error | ||
* @param variable | ||
* @param rules | ||
* @param abolish | ||
*/ | ||
export declare const attemptAsync: <V = any>(variable: V, rules: Record<string, any> | string | string[], abolish?: typeof Abolish | undefined) => Promise<V>; | ||
/** | ||
* check a variable does not throw error | ||
* @param variable | ||
* @param rules | ||
* @param abolish | ||
*/ | ||
export declare const check: <V = any>(variable: V, rules: Record<string, any> | string, abolish?: typeof Abolish | undefined) => [false | ValidationError, V]; | ||
/** | ||
* Checks a variable Asynchronously | ||
* @param variable | ||
* @param rules | ||
* @param abolish | ||
*/ | ||
export declare const checkAsync: <V = any>(variable: V, rules: Record<string, any> | string | string[], abolish?: typeof Abolish | undefined) => Promise<[false | ValidationError, V]>; | ||
/** | ||
* $inLine object generator | ||
@@ -65,0 +36,0 @@ * @param fn |
@@ -6,6 +6,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.skipIfNotDefined = exports.skipIfUndefined = exports.$inline = exports.checkAsync = exports.check = exports.attemptAsync = exports.attempt = exports.ParseRules = exports.Get = exports.Set = exports.Pick = exports.Rule = exports.StartCase = exports.UpperFirst = void 0; | ||
exports.skipIfNotDefined = exports.skipIfUndefined = exports.$inline = exports.ParseRules = exports.abolish_Get = exports.abolish_Set = exports.abolish_Pick = exports.Rule = exports.abolish_StartCase = exports.abolish_UpperFirst = void 0; | ||
const StringToRules_1 = __importDefault(require("./StringToRules")); | ||
const lodash_startcase_1 = __importDefault(require("lodash.startcase")); | ||
const Abolish_1 = __importDefault(require("./Abolish")); | ||
/** | ||
@@ -16,8 +15,8 @@ * Change to string to upperFirst | ||
*/ | ||
function UpperFirst(str) { | ||
function abolish_UpperFirst(str) { | ||
return str[0].toUpperCase() + str.substr(1); | ||
} | ||
exports.UpperFirst = UpperFirst; | ||
exports.abolish_UpperFirst = abolish_UpperFirst; | ||
/** | ||
* StartCase | ||
* abolish_StartCase | ||
* @param str | ||
@@ -27,3 +26,3 @@ * @param abolishInstance | ||
*/ | ||
function StartCase(str, abolishInstance) { | ||
function abolish_StartCase(str, abolishInstance) { | ||
return abolishInstance | ||
@@ -35,3 +34,3 @@ ? abolishInstance.config.useStartCaseInErrors | ||
} | ||
exports.StartCase = StartCase; | ||
exports.abolish_StartCase = abolish_StartCase; | ||
/** | ||
@@ -45,3 +44,2 @@ * Converts an array of rules (string | object)[] to one object rule | ||
* @param rules | ||
* @param options | ||
* @constructor | ||
@@ -74,3 +72,3 @@ */ | ||
exports.Rule = Rule; | ||
function Pick(object, keys) { | ||
function abolish_Pick(object, keys) { | ||
return keys.reduce((obj, key) => { | ||
@@ -83,4 +81,4 @@ if (object && object.hasOwnProperty(key)) { | ||
} | ||
exports.Pick = Pick; | ||
function Set(object, path, value) { | ||
exports.abolish_Pick = abolish_Pick; | ||
function abolish_Set(object, path, value) { | ||
if (Object(object) !== object) | ||
@@ -103,4 +101,4 @@ return object; // When object is not an object | ||
} | ||
exports.Set = Set; | ||
function Get(obj, path, defaultValue) { | ||
exports.abolish_Set = abolish_Set; | ||
function abolish_Get(obj, path, defaultValue) { | ||
if (path.indexOf(".") >= 0) { | ||
@@ -118,3 +116,3 @@ const travel = (regexp) => String.prototype.split | ||
} | ||
exports.Get = Get; | ||
exports.abolish_Get = abolish_Get; | ||
function ParseRules(rules) { | ||
@@ -145,50 +143,2 @@ /** | ||
/** | ||
* Validates a variable | ||
* @param variable | ||
* @param rules | ||
* @param abolish | ||
*/ | ||
const attempt = (variable, rules, abolish) => { | ||
const [e, v] = (abolish ? abolish : Abolish_1.default).validate({ variable }, { variable: rules }); | ||
if (e) | ||
throw new Error(e.message); | ||
return v.variable; | ||
}; | ||
exports.attempt = attempt; | ||
/** | ||
* Validates a variable Asynchronously, Throws error | ||
* @param variable | ||
* @param rules | ||
* @param abolish | ||
*/ | ||
const attemptAsync = async (variable, rules, abolish) => { | ||
const [e, v] = await (abolish ? abolish : Abolish_1.default).validateAsync({ variable }, { variable: rules }); | ||
if (e) | ||
throw new Error(e.message); | ||
return v.variable; | ||
}; | ||
exports.attemptAsync = attemptAsync; | ||
/** | ||
* check a variable does not throw error | ||
* @param variable | ||
* @param rules | ||
* @param abolish | ||
*/ | ||
const check = (variable, rules, abolish) => { | ||
const [e, v] = (abolish ? abolish : Abolish_1.default).validate({ variable }, { variable: rules }); | ||
return [e, v === null || v === void 0 ? void 0 : v.variable]; | ||
}; | ||
exports.check = check; | ||
/** | ||
* Checks a variable Asynchronously | ||
* @param variable | ||
* @param rules | ||
* @param abolish | ||
*/ | ||
const checkAsync = async (variable, rules, abolish) => { | ||
const [e, v] = await (abolish ? abolish : Abolish_1.default).validateAsync({ variable }, { variable: rules }); | ||
return [e, v === null || v === void 0 ? void 0 : v.variable]; | ||
}; | ||
exports.checkAsync = checkAsync; | ||
/** | ||
* $inLine object generator | ||
@@ -195,0 +145,0 @@ * @param fn |
@@ -125,3 +125,3 @@ "use strict"; | ||
/** | ||
* Set an alias for `must` as `required | ||
* abolish_Set an alias for `must` as `required | ||
*/ | ||
@@ -128,0 +128,0 @@ GlobalValidators.required = Object.assign({}, GlobalValidators.must); |
@@ -10,3 +10,3 @@ /** | ||
/** | ||
* Get path of object or return | ||
* abolish_Get path of object or return | ||
* @method | ||
@@ -19,3 +19,3 @@ * @param path | ||
/** | ||
* Get path of current key being validated | ||
* abolish_Get path of current key being validated | ||
* @method | ||
@@ -32,3 +32,3 @@ */ | ||
/** | ||
* Set value to path of object | ||
* abolish_Set value to path of object | ||
* @method | ||
@@ -41,3 +41,3 @@ * @param path | ||
/** | ||
* Set value to this param path | ||
* abolish_Set value to this param path | ||
* @methods | ||
@@ -62,3 +62,3 @@ * @param value | ||
/** | ||
* Get current path but with StartCase | ||
* abolish_Get current path but with abolish_StartCase | ||
*/ | ||
@@ -65,0 +65,0 @@ getPath(): string; |
@@ -19,3 +19,3 @@ "use strict"; | ||
/** | ||
* Get path of object or return | ||
* abolish_Get path of object or return | ||
* @method | ||
@@ -27,6 +27,6 @@ * @param path | ||
get(path, $default = undefined) { | ||
return Functions_1.Get(this.data, path, $default); | ||
return Functions_1.abolish_Get(this.data, path, $default); | ||
} | ||
/** | ||
* Get path of current key being validated | ||
* abolish_Get path of current key being validated | ||
* @method | ||
@@ -47,3 +47,3 @@ */ | ||
/** | ||
* Set value to path of object | ||
* abolish_Set value to path of object | ||
* @method | ||
@@ -55,6 +55,6 @@ * @param path | ||
set(path, value) { | ||
return Functions_1.Set(this.data, path, value); | ||
return Functions_1.abolish_Set(this.data, path, value); | ||
} | ||
/** | ||
* Set value to this param path | ||
* abolish_Set value to this param path | ||
* @methods | ||
@@ -85,8 +85,8 @@ * @param value | ||
/** | ||
* Get current path but with StartCase | ||
* abolish_Get current path but with abolish_StartCase | ||
*/ | ||
getPath() { | ||
return Functions_1.StartCase(this.path); | ||
return Functions_1.abolish_StartCase(this.path); | ||
} | ||
} | ||
module.exports = ObjectModifier; |
@@ -40,1 +40,2 @@ import AbolishError from "./AbolishError"; | ||
}; | ||
export declare type AbolishRule = string | string[] | Record<string, any> | AbolishRule[]; |
"use strict"; | ||
function runThis(option, modifier) { | ||
// Get current string from state. | ||
// abolish_Get current string from state. | ||
const str = modifier.getThis(); | ||
@@ -5,0 +5,0 @@ // @ts-ignore |
58072
1691