altheia-async-data-validator
Version:
A very simple, fast and customizable async data validator
82 lines (81 loc) • 2.54 kB
TypeScript
import { TypeBase } from './validators';
import { ValidatorErrorFormatted, ValidatorErrorRaw, ValidatorTestResult } from './types/tests';
import { AltheiaInstance } from './types/instance';
import { ValidatorConfirm, ValidatorOptions, ValidatorSchema } from './types/validator';
export declare type ValidatorResult = false | ValidatorErrorFormatted[];
export declare type ValidatorCallback = (errors: ValidatorResult) => void;
/**
* Validator class
* new Validator({ foo: 'bar' });
*/
export declare class Validator {
inst: AltheiaInstance;
isValidator: number;
validated: boolean;
_schema: {
[key: string]: TypeBase;
};
_body: any;
_errors: ValidatorErrorFormatted[];
_errorsRaw: ValidatorErrorRaw[];
_confirm: ValidatorConfirm[];
_options: ValidatorOptions;
/**
* Constructor
* @param {object} schema
* @param {object} inst An Altheia instance
*/
constructor(schema: ValidatorSchema, inst: AltheiaInstance);
/**
* Clone a validator
* @return {Validator}
*/
clone(): Validator;
/**
* Assign the body to validate
*
* @param {object} body
* @return {this}
*/
body(body: any): this;
/**
* Declare the schema that describe the body()
*
* @param {object} schema
* @return {this}
*/
schema(schema: ValidatorSchema): this;
/**
* Declare options to change the defaults behaviour of the Validator
*
* @param {object} options
* @return {this}
*/
options(options: ValidatorOptions): this;
/**
* Format any Error Array returned by a test
* @param {object} error
* @param {string} label
* @return {ValidatorErrorFormatted} Formatted error
*/
formatError(error: ValidatorTestResult, label: string): ValidatorErrorFormatted;
private flatten;
/**
* Validate the body against the schema
*
* @param {function} callback
* @return {false | ValidatorErrorFormatted[]} Test resutls
*/
validate(): Promise<ValidatorResult>;
validate(callback: ValidatorCallback): Promise<ValidatorResult>;
validate(body: Record<string, unknown>, callback?: ValidatorCallback): Promise<ValidatorResult>;
/**
* Force the confirmation of one field by an other one
*
* @param {string} initial The first key
* @param {string} comparison The second key
* @return {this}
*/
confirm(initial: string, comparison: string): this;
}
export default Validator;