1
+ import { isUndefined } from '../../utils/shared.utils' ;
1
2
import { Injectable , Optional } from '../../decorators/core' ;
2
3
import { HttpStatus } from '../../enums' ;
3
- import { HttpErrorByCode } from '../../utils/http-error-by-code.util' ;
4
4
import { PipeTransform } from '../../interfaces/features/pipe-transform.interface' ;
5
- import { ParseFileOptions } from './parse-file-options.interface ' ;
5
+ import { HttpErrorByCode } from '../../utils/http-error-by-code.util ' ;
6
6
import { FileValidator } from './file-validator.interface' ;
7
+ import { ParseFileOptions } from './parse-file-options.interface' ;
7
8
8
9
/**
9
10
* Defines the built-in ParseFile Pipe. This pipe can be used to validate incoming files
@@ -19,22 +20,33 @@ import { FileValidator } from './file-validator.interface';
19
20
export class ParseFilePipe implements PipeTransform < any > {
20
21
protected exceptionFactory : ( error : string ) => any ;
21
22
private readonly validators : FileValidator [ ] ;
23
+ private readonly fileIsRequired : boolean ;
22
24
23
25
constructor ( @Optional ( ) options : ParseFileOptions = { } ) {
24
26
const {
25
27
exceptionFactory,
26
28
errorHttpStatusCode = HttpStatus . BAD_REQUEST ,
27
29
validators = [ ] ,
30
+ fileIsRequired,
28
31
} = options ;
29
32
30
33
this . exceptionFactory =
31
34
exceptionFactory ||
32
35
( error => new HttpErrorByCode [ errorHttpStatusCode ] ( error ) ) ;
33
36
34
37
this . validators = validators ;
38
+ this . fileIsRequired = fileIsRequired ?? true ;
35
39
}
36
40
37
41
async transform ( value : any ) : Promise < any > {
42
+ if ( isUndefined ( value ) ) {
43
+ if ( this . fileIsRequired ) {
44
+ throw this . exceptionFactory ( 'File is required' ) ;
45
+ }
46
+
47
+ return value ;
48
+ }
49
+
38
50
if ( this . validators . length ) {
39
51
await this . validate ( value ) ;
40
52
}
0 commit comments