UNPKG

@nestjs/common

Version:

Decorators, pipes, exceptions and utilities for NestJS applications: @Controller(), @Injectable(), @Body(), ValidationPipe, HttpException and more.

82 lines (81 loc) • 3.23 kB
import { FileValidatorContext } from './file-validator-context.interface.js'; import { FileValidator } from './file-validator.interface.js'; import { IFile } from './interfaces/index.js'; type FileTypeValidatorContext = FileValidatorContext<Omit<FileTypeValidatorOptions, 'errorMessage'>>; export type FileTypeValidatorOptions = { /** * Expected file type(s) for validation. Can be a string (MIME type) * or a regular expression to match multiple types. * * A string matches a MIME type that contains it, up to the end of a name: * 'image/png' or 'png' matches 'image/png', and 'audio/wav' does not match * 'audio/wavpack'. * * @example * // Match a single MIME type * fileType: 'image/png' * * @example * // Match multiple types using RegExp * fileType: /^image\/(png|jpeg)$/ */ fileType: string | RegExp; /** * Custom error message displayed when file type validation fails * Can be provided as a static string, or as a factory function * that receives the validation context (file and validator configuration) * and returns a dynamic error message. * * @example * // Static message * new FileTypeValidator({ fileType: 'image/png', errorMessage: 'Only PNG allowed' }) * * @example * // Dynamic message based on file object and validator configuration * new FileTypeValidator({ * fileType: 'image/png', * errorMessage: ctx => `Received file type '${ctx.file?.mimetype}', but expected '${ctx.config.fileType}'` * }) */ errorMessage?: string | ((ctx: FileTypeValidatorContext) => string); /** * If `true`, the validator will skip the magic numbers validation. * This can be useful when you can't identify some files as there are no common magic numbers available for some file types. * @default false */ skipMagicNumbersValidation?: boolean; /** * If `true`, and magic number check fails, fallback to mimetype comparison. * @default false */ fallbackToMimetype?: boolean; /** * If `true`, replaces the client-provided mimetype with the mimetype * detected from the file content using magic number validation. * * @default false */ overrideMimeType?: boolean; }; /** * Defines the built-in FileTypeValidator. It validates incoming files by examining * their magic numbers using the file-type package, providing more reliable file type validation * than just checking the mimetype string. * * @see [File Validators](https://docs.nestjs.com/techniques/file-upload#validators) * * @publicApi */ export declare class FileTypeValidator extends FileValidator<FileTypeValidatorOptions, IFile> { /** * The type detected from the content of each validated file (`null` when * none was), so that the error message can report the type that was * actually checked rather than the one the client declared. Keyed by file, * as the validator is shared by concurrent uploads. */ private readonly detectedMimeTypes; buildErrorMessage(file?: IFile): string; isValid(file?: IFile): Promise<boolean>; private matchesFileType; } export {};