abolish
Version:
A javascript object validator.
108 lines (107 loc) • 3.02 kB
TypeScript
import type { $skipRule, AbolishRule, AbolishSchema, AbolishValidatorFunctionResult, ValidationError, ValidationResult } from "./types";
/**
* Compiled Validator Type
*/
export interface CompiledValidator {
name: string;
option: any;
optionString?: string;
error: string;
async: boolean;
errorFn?: (e: Pick<ValidationError, "code" | "data" | "validator"> & {
value: any;
}) => string;
customError?: boolean;
func: (value: any, option: any) => AbolishValidatorFunctionResult | Promise<AbolishValidatorFunctionResult>;
}
/**
* Abolish Compiled Object Version.
*/
export interface AbolishCompiledObject extends AbolishCompiled {
isObject: true;
}
/**
* Compiled Rule
*/
export interface CompiledRule {
$skip?: $skipRule;
$name?: string;
validators: Record<string, CompiledValidator>;
}
export declare class AbolishCompiled {
/**
* Hold Compiled Object
*/
data: Record<string, CompiledRule>;
/**
* Schema Keys and Included Fields
*/
fields: string[];
/**
* Fields included
*/
includedFields?: string[];
/**
* Allowed Fields when $strict rule is used
*/
allowedFields?: string[];
/**
* If fields has any dot notation set to true
*/
fieldsHasDotNotation: boolean;
/**
* Is Object is true, but if a variable is passed, it will return false
*/
isObject: boolean;
/**
* if there is an async validator, set to true
*/
async: boolean;
input: AbolishRule | AbolishSchema;
/**
* Constructor
* @param input
*/
constructor(input: AbolishRule | AbolishSchema);
/**
* Validate Compiled Schema
* @param data
*/
validateObject<R = Record<string, any>>(data: Record<string, any>): ValidationResult<R>;
validateObjectAsync<R = Record<string, any>>(data: Record<string, any>): Promise<ValidationResult<R>>;
/**
* Validate a variable using a variable compiled input
* @param variable Variable to validate
* @returns
*/
validateVariable<T>(variable: T): ValidationResult<T>;
/**
* validateVariable async version
* @param variable Variable to validate
* @returns
*/
validateVariableAsync<T>(variable: T): Promise<ValidationResult<T>>;
validate<T>(value: T): ValidationResult<T>;
validateAsync<T>(value: T): Promise<ValidationResult<T>>;
/**
* Get `this.input` as AbolishRule
*/
getInputRule(): AbolishRule;
/**
* Get `this.input` as AbolishSchema
*/
getInputSchema(): AbolishSchema;
/**
* Change a fields validator option
* @param fieldName
* @param validatorName
* @param option
*/
setValidatorOption(validatorName: string, option: any, fieldName?: string): this;
/**
* Copy current compiled instance
* This is useful when you want to use the same compiled input for multiple validation
* It prevents memory leak
*/
copy(): this;
}