Skip to content

Commit

Permalink
docs(common): add validators docs
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagomini committed Jun 1, 2022
1 parent eb2ef1a commit b059f50
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/common/pipes/file/file-type.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ export type FileTypeValidatorOptions = {
fileType: string;
};

/**
* Defines the built-in FileType File Validator
*
* @see [File Validators](https://docs.nestjs.com/techniques/file-upload#validators)
*
* @publicApi
*/
export class FileTypeValidator extends FileValidator<FileTypeValidatorOptions> {
buildErrorMessage(): string {
return `Validation failed (expected type is ${this.validationOptions.fileType})`;
Expand Down
12 changes: 12 additions & 0 deletions packages/common/pipes/file/file-validator.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
/**
* Interface describing FileValidators, which can be added to a {@link ParseFilePipe}.
*/
export abstract class FileValidator<TValidationOptions = Record<string, any>> {
constructor(protected readonly validationOptions: TValidationOptions) {}

/**
* Indicates if this file should be considered valid, according to the options passed in the constructor.
* @param file the file from the request object
*/
abstract isValid(file?: any): boolean;

/**
* Builds an error message in case the validation fails.
* @param file the file from the request object
*/
abstract buildErrorMessage(file: any): string;
}
7 changes: 7 additions & 0 deletions packages/common/pipes/file/max-file-size.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ export type MaxFileSizeValidatorOptions = {
maxSize: number;
};

/**
* Defines the built-in MaxSize File Validator
*
* @see [File Validators](https://docs.nestjs.com/techniques/file-upload#validators)
*
* @publicApi
*/
export class MaxFileSizeValidator extends FileValidator<MaxFileSizeValidatorOptions> {
buildErrorMessage(): string {
return `Validation failed (expected size is less than ${this.validationOptions.maxSize})`;
Expand Down
13 changes: 13 additions & 0 deletions packages/common/pipes/file/parse-file.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ import { PipeTransform } from '../../interfaces/features/pipe-transform.interfac
import { ParseFileOptions } from './parse-file-options.interface';
import { FileValidator } from './file-validator.interface';

/**
* Defines the built-in ParseFile Pipe. This pipe can be used to validate incoming files
* with `@UploadedFile()` decorator. You can use either other specific built-in validators
* or provide one of your own, simply implementing it through {@link FileValidator}
* interface and adding it to ParseFilePipe's constructor.
*
* @see [Built-in Pipes](https://docs.nestjs.com/pipes#built-in-pipes)
*
* @publicApi
*/
@Injectable()
export class ParseFilePipe implements PipeTransform<any> {
protected exceptionFactory: (error: string) => any;
Expand Down Expand Up @@ -43,6 +53,9 @@ export class ParseFilePipe implements PipeTransform<any> {
return file;
}

/**
* @returns list of validators used in this pipe.
*/
getValidators() {
return this.validators;
}
Expand Down

0 comments on commit b059f50

Please sign in to comment.