UNPKG

altheia-async-data-validator

Version:

A very simple, fast and customizable async data validator

78 lines (77 loc) 2.05 kB
import { createTest, createTestResult } from '../utils/createTest'; import { AltheiaInstance } from '../types/instance'; import { ValidatorInternalTest, TestFunction, ValidatorTestResult } from '../types/tests'; /** * All type inherit this Class */ export declare abstract class TypeBase { inst: AltheiaInstance; tests: (() => ValidatorInternalTest)[]; name?: string; _required: boolean; _needCast: boolean; _noEmpty?: boolean; /** * Constructor * * @param {Altheia} inst */ constructor(inst?: AltheiaInstance); abstract _cast(value: any): void; /** * Clone this class */ clone<T extends this>(): T; /** * Add a test * * @param name * @param func * @param args */ test(name: string, func: TestFunction, args?: {}): void; createTest: typeof createTest; createTestResult: typeof createTestResult; /** * Validate a value based on all tests added * @param {mixed} toTest * @param {Function} callback */ validate(toTest: any, callback?: (value: false | ValidatorTestResult) => void): Promise<false | ValidatorTestResult>; /** * Force the value to be non-null */ required(): this; /** * Force the value to be casted before any check */ cast(): this; /** * Custom validator * * @param {string} name * @param {Function} callback * @param {Function} message */ custom(name: string, callback: TestFunction): this; /** * Presence validation * * @param {mixed} toTest */ private presence; /** * If validation * * @param {function} options.test * @param {function} options.then * @param {function} options.otherwise */ if<T extends this>({ test, then, otherwise, }: { test: (chain: T) => TypeBase; then: (chain: T) => TypeBase; otherwise: (chain: T) => TypeBase; }): this; private testToTestResult; } export default TypeBase;