import { AwilixContainer } from "awilix" /** * Generic validation interface used to run validation logic on every line or record. * All different validation objects should implement this interface */ export interface ICsvValidator { /** * * @param value value of column or property * @param context includes contextual information such as line number, line, etc. */ validate: ( value: TBuiltLine, context: CsvParserContext ) => Promise } export type CsvParserContext = LineContext & { column: string } export type LineContext = { lineNumber: number line: TLine } /** * Abstract class implementation of the IValidator interface. * All validation objects part of the schema should extend this class. */ export abstract class AbstractCsvValidator implements ICsvValidator { constructor(protected readonly container: AwilixContainer) {} abstract validate( builtLine: TBuiltLine, context: CsvParserContext ): Promise } export type CsvSchemaColumn< TCsvLine, TBuiltLine, NameAsOptional = false > = (NameAsOptional extends false ? { name: string } : { name?: string }) & { required?: boolean validator?: AbstractCsvValidator } & ( | { mapTo?: string transform?: ColumnTransformer } | { match?: RegExp reducer?: ColumnReducer transform?: ColumnTransformer } ) export type ColumnTransformer = ( value: string, context: CsvParserContext ) => unknown export type ColumnReducer = ( builtLine: TBuiltLine, key: string, value: string, context: CsvParserContext ) => TBuiltLine export type CsvSchema = { columns: CsvSchemaColumn[] }