11import { Type } from '@nestjs/common' ;
22import {
33 applyIsOptionalDecorator ,
4+ applyValidateIfDefinedDecorator ,
45 inheritPropertyInitializers ,
56 inheritTransformationMetadata ,
67 inheritValidationMetadata
@@ -15,7 +16,25 @@ import { clonePluginMetadataFactory } from './mapped-types.utils';
1516
1617const modelPropertiesAccessor = new ModelPropertiesAccessor ( ) ;
1718
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+
1938 const fields = modelPropertiesAccessor . getModelProperties ( classRef . prototype ) ;
2039
2140 abstract class PartialTypeClass {
@@ -30,7 +49,7 @@ export function PartialType<T>(classRef: Type<T>): Type<Partial<T>> {
3049 if ( keysWithValidationConstraints ) {
3150 keysWithValidationConstraints
3251 . filter ( ( key ) => ! fields . includes ( key ) )
33- . forEach ( ( key ) => applyIsOptionalDecorator ( PartialTypeClass , key ) ) ;
52+ . forEach ( ( key ) => applyPartialDecoratorFn ( PartialTypeClass , key ) ) ;
3453 }
3554
3655 inheritTransformationMetadata ( classRef , PartialTypeClass ) ;
@@ -48,7 +67,7 @@ export function PartialType<T>(classRef: Type<T>): Type<Partial<T>> {
4867 PartialTypeClass [ METADATA_FACTORY_NAME ] ( )
4968 ) ;
5069 pluginFields . forEach ( ( key ) =>
51- applyIsOptionalDecorator ( PartialTypeClass , key )
70+ applyPartialDecoratorFn ( PartialTypeClass , key )
5271 ) ;
5372 }
5473
@@ -65,7 +84,7 @@ export function PartialType<T>(classRef: Type<T>): Type<Partial<T>> {
6584 required : false
6685 } ) ;
6786 decoratorFactory ( PartialTypeClass . prototype , key ) ;
68- applyIsOptionalDecorator ( PartialTypeClass , key ) ;
87+ applyPartialDecoratorFn ( PartialTypeClass , key ) ;
6988 } ) ;
7089 }
7190 applyFields ( fields ) ;
0 commit comments