UNPKG

@resk/core

Version:

An innovative TypeScript framework that empowers developers to build applications with a fully decorator-based architecture for efficient resource management. By combining the power of decorators with a resource-oriented design, DecorRes enhances code cla

398 lines (397 loc) 12.3 kB
/** * ### File Rule * * Validates that the field under validation is a file object. * * @example * ```typescript * // Class validation * class UploadForm { * @IsRequired * @IsFile * document: File; * } * ``` * * @param options - Validation options containing value and context * @returns Promise resolving to true if valid, rejecting with error message if invalid * * @since 1.22.0 * @public */ export declare const IsFile: PropertyDecorator; /** * ### MaxFileSize Rule * * Validates that the file size does not exceed the specified maximum size in bytes. * * #### Parameters * - Maximum file size in bytes (number) * * @example * ```typescript * // Class validation - 5MB max * class UploadForm { * @MaxFileSize(5242880) // 5MB in bytes * document: File; * } * ``` * * @param options - Validation options with rule parameters * @param options.ruleParams - Array containing maximum file size in bytes * @returns Promise resolving to true if valid, rejecting with error message if invalid * * @since 1.22.0 * @public */ export declare const MaxFileSize: (ruleParameters: [size: number]) => PropertyDecorator; /** * ### FileType Rule * * Validates that the file has one of the specified MIME types. * * #### Parameters * - Allowed MIME types (array of strings) * * @example * ```typescript * // Class validation * class ImageUpload { * @IsFileType(['image/jpeg', 'image/png', 'image/gif']) * image: File; * } * ``` * * @param options - Validation options with rule parameters * @param options.ruleParams - Array of allowed MIME types * @returns Promise resolving to true if valid, rejecting with error message if invalid * * @since 1.22.0 * @public */ export declare const IsFileType: (ruleParameters: string[]) => PropertyDecorator; /** * ### Image Rule * * Validates that the file is an image (checks MIME type). * * @example * ```typescript * // Class validation * class ProfilePicture { * @IsImage * avatar: File; * } * ``` * * @param options - Validation options containing value and context * @returns Promise resolving to true if valid, rejecting with error message if invalid * * @since 1.22.0 * @public */ export declare const IsImage: PropertyDecorator; /** * ### FileExtension Rule * * Validates that the file has one of the specified extensions. * * #### Parameters * - Allowed file extensions (array of strings, without dots) * * @example * ```typescript * // Class validation * class DocumentUpload { * @IsFileExtension(['pdf', 'doc', 'docx']) * document: File; * } * ``` * * @param options - Validation options with rule parameters * @param options.ruleParams - Array of allowed file extensions * @returns Promise resolving to true if valid, rejecting with error message if invalid * * @since 1.22.0 * @public */ export declare const IsFileExtension: (ruleParameters: string[]) => PropertyDecorator; /** * ### MinFileSize Rule * * Validates that the file size is at least the specified minimum size in bytes. * * #### Parameters * - Minimum file size in bytes (number) * * @example * ```typescript * // Class validation - minimum 1KB * class UploadForm { * @MinFileSize(1024) // 1KB in bytes * document: File; * } * ``` * * @param options - Validation options with rule parameters * @param options.ruleParams - Array containing minimum file size in bytes * @returns Promise resolving to true if valid, rejecting with error message if invalid * * @since 1.22.0 * @public */ export declare const MinFileSize: (ruleParameters: [minSize: number]) => PropertyDecorator; declare module "../types" { interface IValidatorRulesMap<Context = unknown> { /** * ### File Rule * * Validates that the field under validation is a file object. * * @example * ```typescript * // Valid file objects * await Validator.validate({ * value: { name: 'test.txt', size: 1024, type: 'text/plain' }, * rules: ['File'] * }); // ✓ Valid * * // Invalid examples * await Validator.validate({ * value: 'not a file', * rules: ['File'] * }); // ✗ Invalid * * await Validator.validate({ * value: null, * rules: ['File'] * }); // ✗ Invalid * * // Class validation * class UploadForm { * @Required * @File * document: File; * } * ``` * * @param options - Validation options containing value and context * @returns Promise resolving to true if valid, rejecting with error message if invalid * * @since 1.22.0 * @public */ File: IValidatorRuleParams<[], Context>; /** * ### MaxFileSize Rule * * Validates that the file size does not exceed the specified maximum size in bytes. * * #### Parameters * - Maximum file size in bytes (number) * * @example * ```typescript * // Valid examples * await Validator.validate({ * value: { name: 'small.txt', size: 1024, type: 'text/plain' }, * rules: ['MaxFileSize[2048]'] * }); // ✓ Valid (file <= 2KB) * * // Invalid examples * await Validator.validate({ * value: { name: 'large.txt', size: 5242880, type: 'text/plain' }, * rules: ['MaxFileSize[2048]'] * }); // ✗ Invalid (file > 2KB) * * await Validator.validate({ * value: 'not a file', * rules: ['MaxFileSize[2048]'] * }); // ✗ Invalid * * // Class validation - 5MB max * class UploadForm { * @MaxFileSize(5242880) // 5MB in bytes * document: File; * } * ``` * * @param options - Validation options with rule parameters * @param options.ruleParams - Array containing maximum file size in bytes * @returns Promise resolving to true if valid, rejecting with error message if invalid * * @since 1.22.0 * @public */ MaxFileSize: IValidatorRuleParams<[size: number], Context>; /** * ### FileType Rule * * Validates that the file has one of the specified MIME types. * * #### Parameters * - Allowed MIME types (array of strings) * * @example * ```typescript * // Valid examples * await Validator.validate({ * value: { name: 'photo.jpg', size: 1024, type: 'image/jpeg' }, * rules: ['FileType[image/jpeg,image/png]'] * }); // ✓ Valid * * await Validator.validate({ * value: { name: 'doc.pdf', size: 1024, type: 'application/pdf' }, * rules: ['FileType[application/pdf]'] * }); // ✓ Valid * * // Invalid examples * await Validator.validate({ * value: { name: 'script.js', size: 1024, type: 'application/javascript' }, * rules: ['FileType[image/jpeg,image/png]'] * }); // ✗ Invalid * * // Class validation * class ImageUpload { * @IsFileType(['image/jpeg', 'image/png', 'image/gif']) * image: File; * } * ``` * * @param options - Validation options with rule parameters * @param options.ruleParams - Array of allowed MIME types * @returns Promise resolving to true if valid, rejecting with error message if invalid * * @since 1.22.0 * @public */ FileType: IValidatorRuleParams<string[], Context>; /** * ### Image Rule * * Validates that the file is an image (checks MIME type). * * @example * ```typescript * // Valid examples * await Validator.validate({ * value: { name: 'photo.jpg', size: 1024, type: 'image/jpeg' }, * rules: ['Image'] * }); // ✓ Valid * * await Validator.validate({ * value: { name: 'pic.png', size: 1024, type: 'image/png' }, * rules: ['Image'] * }); // ✓ Valid * * // Invalid examples * await Validator.validate({ * value: { name: 'doc.pdf', size: 1024, type: 'application/pdf' }, * rules: ['Image'] * }); // ✗ Invalid * * await Validator.validate({ * value: 'not a file', * rules: ['Image'] * }); // ✗ Invalid * * // Class validation * class ProfilePicture { * @IsImage * avatar: File; * } * ``` * * @param options - Validation options containing value and context * @returns Promise resolving to true if valid, rejecting with error message if invalid * * @since 1.22.0 * @public */ Image: IValidatorRuleParams<[], Context>; /** * ### FileExtension Rule * * Validates that the file has one of the specified extensions. * * #### Parameters * - Allowed file extensions (array of strings, without dots) * * @example * ```typescript * // Valid examples * await Validator.validate({ * value: { name: 'document.pdf', size: 1024, type: 'application/pdf' }, * rules: ['FileExtension[pdf,doc,docx]'] * }); // ✓ Valid * * await Validator.validate({ * value: { name: 'script.js', size: 1024, type: 'application/javascript' }, * rules: ['FileExtension[js,ts]'] * }); // ✓ Valid * * // Invalid examples * await Validator.validate({ * value: { name: 'image.exe', size: 1024, type: 'application/octet-stream' }, * rules: ['FileExtension[pdf,doc,docx]'] * }); // ✗ Invalid * * // Class validation * class DocumentUpload { * @IsFileExtension(['pdf', 'doc', 'docx']) * document: File; * } * ``` * * @param options - Validation options with rule parameters * @param options.ruleParams - Array of allowed file extensions * @returns Promise resolving to true if valid, rejecting with error message if invalid * * @since 1.22.0 * @public */ FileExtension: IValidatorRuleParams<string[], Context>; /** * ### MinFileSize Rule * * Validates that the file size is at least the specified minimum size in bytes. * * #### Parameters * - Minimum file size in bytes (number) * * @example * ```typescript * // Valid examples * await Validator.validate({ * value: { name: 'file.txt', size: 2048, type: 'text/plain' }, * rules: ['MinFileSize[1024]'] * }); // ✓ Valid (file >= 1KB) * * // Invalid examples * await Validator.validate({ * value: { name: 'small.txt', size: 512, type: 'text/plain' }, * rules: ['MinFileSize[1024]'] * }); // ✗ Invalid (file < 1KB) * * await Validator.validate({ * value: 'not a file', * rules: ['MinFileSize[1024]'] * }); // ✗ Invalid * * // Class validation - minimum 1KB * class UploadForm { * @MinFileSize(1024) // 1KB in bytes * document: File; * } * ``` * * @param options - Validation options with rule parameters * @param options.ruleParams - Array containing minimum file size in bytes * @returns Promise resolving to true if valid, rejecting with error message if invalid * * @since 1.22.0 * @public */ MinFileSize: IValidatorRuleParams<[minSize: number], Context>; } }