express-image-validator
Version:
Validator of various image parameters in Express.js applications
74 lines (73 loc) • 2.65 kB
TypeScript
import { UploadedFile } from 'express-fileupload';
import { ValidationOptions } from '../types/options';
/**
* The validation error produced by image validators.
*/
export type ValidationError = {
/**
* Error message describing why validation failed.
*/
msg: string;
/**
* The name of the validated field.
*/
field: string;
/**
* The actual value that caused the error.
*/
value: unknown;
/**
* Name of the uploaded file if available.
*/
filename: string | null;
};
/**
* The result of a single validation function.
*/
export type ValidationResult = {
/**
* A list of validation errors.
*/
errors: ValidationError[];
/**
* A flag indicating that further validation should be stopped.
*/
breakValidation?: boolean;
};
/**
* Function that validates all uploaded files for a specific field.
* Used when the validation logic depends on the set of files rather than on
* each file individually
* (e.g. checking total number of files or presence of at least one valid file).
* @param { string } field Name of the validated field.
* @param { UploadedFile[] } files List of uploaded files to validate.
* @param { ValidationOptions } options Validation options.
* @returns { ValidationResult | Promise<ValidationResult> } The validation result containing possible errors.
*/
export type FieldValidatorFunction = (field: string, files: UploadedFile[], options: ValidationOptions) => ValidationResult | Promise<ValidationResult>;
/**
* Function that validates a single uploaded file.
* Used when the validation logic applies to each file individuall
* (e.g. checking file size or MIME type).
*
* @param { string } field Name of the validated field.
* @param { import('express-fileupload').UploadedFile } file The uploaded file to validate.
* @param { ValidationOptions } options Validation options.
* @returns { ValidationResult | Promise<ValidationResult> } The validation result containing possible errors.
*/
export type FileValidatorFunction = (field: string, file: UploadedFile, options: ValidationOptions) => ValidationResult | Promise<ValidationResult>;
/**
* Container for validator functions by scope.
* Field-level validators check overall field constraints (e.g., required, limit).
* File-level validators check each file individually.
*/
export type ValidatorFunctions = {
/**
* Validators executed on the entire set of files for a field.
*/
fieldLevel?: FieldValidatorFunction[];
/**
* Validators executed on each file separately.
*/
fileLevel?: FileValidatorFunction[];
};