1
1
import { Type } from '@nestjs/common' ;
2
2
import {
3
3
applyIsOptionalDecorator ,
4
+ applyValidateIfDefinedDecorator ,
4
5
inheritPropertyInitializers ,
5
6
inheritTransformationMetadata ,
6
7
inheritValidationMetadata
@@ -15,7 +16,25 @@ import { clonePluginMetadataFactory } from './mapped-types.utils';
15
16
16
17
const modelPropertiesAccessor = new ModelPropertiesAccessor ( ) ;
17
18
18
- export function PartialType < T > ( classRef : Type < T > ) : Type < Partial < T > > {
19
+ export function PartialType < T > (
20
+ classRef : Type < T > ,
21
+ /**
22
+ * Configuration options.
23
+ */
24
+ options : {
25
+ /**
26
+ * If true, validations will be ignored on a property if it is either null or undefined. If
27
+ * false, validations will be ignored only if the property is undefined.
28
+ * @default true
29
+ */
30
+ skipNullProperties ?: boolean ;
31
+ } = { }
32
+ ) : Type < Partial < T > > {
33
+ const applyPartialDecoratorFn =
34
+ options . skipNullProperties === false
35
+ ? applyValidateIfDefinedDecorator
36
+ : applyIsOptionalDecorator ;
37
+
19
38
const fields = modelPropertiesAccessor . getModelProperties ( classRef . prototype ) ;
20
39
21
40
abstract class PartialTypeClass {
@@ -30,7 +49,7 @@ export function PartialType<T>(classRef: Type<T>): Type<Partial<T>> {
30
49
if ( keysWithValidationConstraints ) {
31
50
keysWithValidationConstraints
32
51
. filter ( ( key ) => ! fields . includes ( key ) )
33
- . forEach ( ( key ) => applyIsOptionalDecorator ( PartialTypeClass , key ) ) ;
52
+ . forEach ( ( key ) => applyPartialDecoratorFn ( PartialTypeClass , key ) ) ;
34
53
}
35
54
36
55
inheritTransformationMetadata ( classRef , PartialTypeClass ) ;
@@ -48,7 +67,7 @@ export function PartialType<T>(classRef: Type<T>): Type<Partial<T>> {
48
67
PartialTypeClass [ METADATA_FACTORY_NAME ] ( )
49
68
) ;
50
69
pluginFields . forEach ( ( key ) =>
51
- applyIsOptionalDecorator ( PartialTypeClass , key )
70
+ applyPartialDecoratorFn ( PartialTypeClass , key )
52
71
) ;
53
72
}
54
73
@@ -65,7 +84,7 @@ export function PartialType<T>(classRef: Type<T>): Type<Partial<T>> {
65
84
required : false
66
85
} ) ;
67
86
decoratorFactory ( PartialTypeClass . prototype , key ) ;
68
- applyIsOptionalDecorator ( PartialTypeClass , key ) ;
87
+ applyPartialDecoratorFn ( PartialTypeClass , key ) ;
69
88
} ) ;
70
89
}
71
90
applyFields ( fields ) ;
0 commit comments