base con autenticacion, registro, modulo encuestas
This commit is contained in:
38
apps/api/src/common/pipes/file-size-validator.pipe.ts
Normal file
38
apps/api/src/common/pipes/file-size-validator.pipe.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { FileValidator } from '@nestjs/common';
|
||||
import { IFile } from '@nestjs/common/pipes/file/interfaces';
|
||||
|
||||
export interface FileSizeValidatorOptions {
|
||||
fileSize: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the built-in FileType File Validator. It validates incoming files mime-type
|
||||
* matching a string or a regular expression. Note that this validator uses a naive strategy
|
||||
* to check the mime-type and could be fooled if the client provided a file with renamed extension.
|
||||
* (for instance, renaming a 'malicious.bat' to 'malicious.jpeg'). To handle such security issues
|
||||
* with more reliability, consider checking against the file's [magic-numbers](https://en.wikipedia.org/wiki/Magic_number_%28programming%29)
|
||||
*
|
||||
* @see [File Validators](https://docs.nestjs.com/techniques/file-upload#validators)
|
||||
*
|
||||
* @publicApi
|
||||
*/
|
||||
export class FileSizeValidatorPipe extends FileValidator<
|
||||
FileSizeValidatorOptions,
|
||||
IFile
|
||||
> {
|
||||
buildErrorMessage(): string {
|
||||
return `Max file size is ${(this.validationOptions.fileSize * 0.000001).toFixed()} Mb`;
|
||||
}
|
||||
|
||||
isValid(file?: IFile): boolean {
|
||||
if (!this.validationOptions) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return (
|
||||
!!file &&
|
||||
'mimetype' in file &&
|
||||
+file.size < this.validationOptions.fileSize
|
||||
);
|
||||
}
|
||||
}
|
||||
38
apps/api/src/common/pipes/file-type-validator.pipe.ts
Normal file
38
apps/api/src/common/pipes/file-type-validator.pipe.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { FileValidator } from '@nestjs/common';
|
||||
import { IFile } from '@nestjs/common/pipes/file/interfaces';
|
||||
|
||||
export interface FileTypeValidatorOptions {
|
||||
fileType: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the built-in FileType File Validator. It validates incoming files mime-type
|
||||
* matching a string or a regular expression. Note that this validator uses a naive strategy
|
||||
* to check the mime-type and could be fooled if the client provided a file with renamed extension.
|
||||
* (for instance, renaming a 'malicious.bat' to 'malicious.jpeg'). To handle such security issues
|
||||
* with more reliability, consider checking against the file's [magic-numbers](https://en.wikipedia.org/wiki/Magic_number_%28programming%29)
|
||||
*
|
||||
* @see [File Validators](https://docs.nestjs.com/techniques/file-upload#validators)
|
||||
*
|
||||
* @publicApi
|
||||
*/
|
||||
export class FileTypeValidatorPipe extends FileValidator<
|
||||
FileTypeValidatorOptions,
|
||||
IFile
|
||||
> {
|
||||
buildErrorMessage(): string {
|
||||
return `File must be ${this.validationOptions.fileType}`;
|
||||
}
|
||||
|
||||
isValid(file?: IFile): boolean {
|
||||
if (!this.validationOptions) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return (
|
||||
!!file &&
|
||||
'mimetype' in file &&
|
||||
this.validationOptions.fileType.includes(file.mimetype)
|
||||
);
|
||||
}
|
||||
}
|
||||
3
apps/api/src/common/pipes/index.ts
Normal file
3
apps/api/src/common/pipes/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './file-size-validator.pipe';
|
||||
export * from './file-type-validator.pipe';
|
||||
export * from './zod-validator.pipe';
|
||||
17
apps/api/src/common/pipes/zod-validator.pipe.ts
Normal file
17
apps/api/src/common/pipes/zod-validator.pipe.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { BadRequestException, PipeTransform } from '@nestjs/common';
|
||||
import { ZodSchema, z } from 'zod';
|
||||
|
||||
export class ZodValidatorPipe implements PipeTransform {
|
||||
constructor(private schema: ZodSchema) {}
|
||||
transform(
|
||||
value: unknown,
|
||||
// metadata: ArgumentMetadata,
|
||||
): z.infer<typeof this.schema> {
|
||||
const validateFields = this.schema.safeParse(value);
|
||||
if (!validateFields.success)
|
||||
throw new BadRequestException({
|
||||
errors: validateFields.error.flatten().fieldErrors,
|
||||
});
|
||||
return validateFields.data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user