import ImportRecord from './ImportRecord'; import { Pipe } from './PipeParser'; import Provider from './Provider'; export type ValidatorCallback = (record: ImportRecord, key: string, ...args: string[]) => Promise | true | string; export type ValidatorWithContextCallback = (context: ImportRecord[], key: string, ...args: string[]) => Promise | void; type ValidatorRegistryItem = { withContext: boolean; validator: ValidatorCallback | ValidatorWithContextCallback; }; export default class Validator { private throttle; private provider; /** * Registry of validators. * * A validator is a function that takes a record, a key, and a set of arguments and returns a boolean or a string. * * Some rules to have in mind when creating a validator: * - The validator should return a boolean or a string. * - The validator should not modify the record directly. * - The validator should not throw an error. Instead, it should return the string error * or true if the validation passes. * - Maintain an alphabetical order when adding validators. */ private registry; setThrottle(throttle: number): void; validate(record: ImportRecord, rules: { [key: string]: Pipe[] | null; } | null): Promise; /** * Validate a records against a set of rules. * * Rules can be a string of pipe-separated rules, or an array of Pipe objects. * * @param record The Import Record to be validated * @param rules The rules to be applied to the record */ validateMany(records: ImportRecord[], rules: { [key: string]: Pipe[] | null; } | null): Promise; validateWithContext(records: ImportRecord[], rules: { [key: string]: Pipe[] | null; } | null): Promise; /** * Returns the validators applicable for the specified field * * @param field The field to get the validators for * @param rules The rules to get the validators from */ getValidatorsForField(field: string, rules: { [key: string]: Pipe[] | null; } | null): Pipe[] | null; /** * Adds a new validator to the registry. * * Will throw an error if the validator already exists. * * @param name Validator's name * @param validator Validator's function */ addValidator(name: string, validator: ValidatorCallback | ValidatorWithContextCallback, withContext?: boolean): void; getValidator(name: string): ValidatorRegistryItem; /** * Go through the provided validators and checks with the registry to see if the need context. * The key is the field name, and the value is an array of validators. * @param validators */ hasAnyWithContext(validators?: { [key: string]: Pipe[]; }): boolean; setProvider(provider: Provider): void; } export {};