abolish
Version:
A javascript object validator.
199 lines (198 loc) • 6.57 kB
TypeScript
import type { AbolishRule, AbolishSchema, AbolishValidator, ValidationError, ValidationResult } from "./types";
import { AbolishCompiled, AbolishCompiledObject } from "./Compiler";
export declare class AttemptError extends Error {
error: ValidationError;
constructor(e: ValidationError);
static instanceOf(e: any): boolean;
}
/**
* Abolish Super Rules and Keys
*/
export declare const SuperKeys: Readonly<{
Wildcards: Set<string>;
Fields: Set<string>;
Rules: Set<string>;
}>;
interface AbolishConfig {
useStartCaseInErrors?: boolean;
}
/**
* Abolish Class
* @class
*/
declare class Abolish {
validators: Record<string, AbolishValidator>;
config: AbolishConfig;
/**
* Get global validators
*/
static getGlobalValidators(): Record<string, AbolishValidator>;
/**
* Get global validators list
*/
static getGlobalValidatorsList(): string[];
/**
* Add single global validator
* @param validator
*/
static addGlobalValidator(validator: AbolishValidator): typeof Abolish;
/**
* Add multiple global validators
* @param validators
*/
static addGlobalValidators(validators: AbolishValidator[] | Record<string, AbolishValidator>): typeof Abolish;
/**
* Toggle start case in error.
* @param value
*/
useStartCaseInErrors(value?: boolean): this;
/**
* addValidator
* @description
* Add validator or array of validators
* @param validator
*/
addValidator(validator: AbolishValidator): this;
/**
* addValidators
* @description
* Add validator or array of validators
* @param validators
*/
addValidators(validators: AbolishValidator[] | Record<string, AbolishValidator>): this;
/**
* Validate
* @description
* Validates given object with rules defined on Abolish instance
* @param {object} object
* @param {object} rules
*/
static validate<R extends Record<string, any> = Record<string, any>>(object: Record<string, any>, rules: AbolishSchema<R>): ValidationResult<R>;
static validate<R extends Record<string, any>>(object: Record<string, any>, rules: AbolishCompiledObject): ValidationResult<R>;
/**
* Validate Async
*
* Waits for all validation defined
* @param object
* @param rules
* @return {Promise<ValidationResult>}
*/
static validateAsync<R extends Record<string, any> = Record<string, any>>(object: Record<string, any>, rules: Record<keyof R | string, any>): Promise<ValidationResult<R>>;
/**
* Validate
* @description
* Validates given object with rules defined on Abolish instance
* @param {object} object
* @param {object} rules
* @param {boolean} isAsync
*/
validate<R extends Record<string, any> = Record<string, any>>(object: Record<string, any>, rules: AbolishSchema<R>, isAsync?: boolean): ValidationResult<R>;
validate<R extends Record<string, any>>(object: Record<string, any>, rules: AbolishCompiledObject, isAsync?: boolean): ValidationResult<R>;
/**
* Validate Async
*
* Waits for all validation defined
* @param object
* @param rules
* @return {Promise<ValidationResult>}
*/
validateAsync<R extends Record<string, any> = Record<string, any>>(object: Record<string, any>, rules: AbolishSchema<R>): Promise<ValidationResult<R>>;
validateAsync<R extends Record<string, any>>(object: Record<string, any>, rules: AbolishCompiledObject): Promise<ValidationResult<R>>;
/**
* check a variable does not throw error
* @param variable
* @param rules
*/
check<V = any>(variable: V, rules: AbolishRule | AbolishCompiled): ValidationResult<V>;
/**
* Static Check
* @param variable
* @param rules
*/
static check<V = any>(variable: V, rules: AbolishRule | AbolishCompiled): ValidationResult<V>;
/**
* Checks a variable Asynchronously
* @param variable
* @param rules
*/
checkAsync<V = any>(variable: V, rules: AbolishRule | AbolishCompiled): Promise<ValidationResult<V>>;
/**
* Static Check Async
* @param variable
* @param rules
*/
static checkAsync<V = any>(variable: V, rules: AbolishRule): Promise<ValidationResult<V>>;
/**
* Validates a variable
* @param variable
* @param rules
*/
attempt<V = any>(variable: V, rules: AbolishRule | AbolishCompiled): V;
/**
* Static Attempt
* @param variable
* @param rules
* @param abolish
*/
static attempt<V = any>(variable: V, rules: AbolishRule | AbolishCompiled): V;
/**
* Validates a variable Asynchronously, Throws error
* @param variable
* @param rules
*/
attemptAsync<V = any>(variable: V, rules: AbolishRule | AbolishCompiled): Promise<V>;
/**
* Validates a variable Asynchronously, Throws error
* @param variable
* @param rules
*/
static attemptAsync<V = any>(variable: V, rules: AbolishRule): Promise<V>;
/**
* test a variable, returns boolean
* @param variable
* @param rules
*/
test<V = any>(variable: V, rules: AbolishRule | AbolishCompiled): boolean;
/**
* Static Check
* @param variable
* @param rules
*/
static test<V = any>(variable: V, rules: AbolishRule | AbolishCompiled): boolean;
/**
* Checks a variable Asynchronously
* @param variable
* @param rules
*/
testAsync<V = any>(variable: V, rules: AbolishRule): Promise<boolean>;
/**
* Static Check Async
* @param variable
* @param rules
*/
static testAsync<V = any>(variable: V, rules: AbolishRule): Promise<boolean>;
/**
* Compile a input
* @param schema
* @param CustomAbolish
*/
static compileObject<S extends AbolishSchema>(schema: S, CustomAbolish?: TypeOfAbolishOrInstance): AbolishCompiledObject;
/**
* Compile for a variable
* @param rule
* @param CustomAbolish
*/
static compile(rule: AbolishRule, CustomAbolish?: TypeOfAbolishOrInstance): AbolishCompiled;
}
export type TypeOfAbolishOrInstance = typeof Abolish | InstanceType<typeof Abolish>;
/**
* Check if a variable can be considered as an Abolish Class
* @param $class
*/
export declare function isAbolishClass($class: any): boolean;
/**
* Check if a variable can be considered as an Abolish Instance
* @param instance
*/
export declare function isAbolishInstance(instance: any): boolean;
export default Abolish;