Skip to content

Commit 468ccba

Browse files
authored
Merge branch 'nestjs:master' into feature/add-response-decorator-from-comment
2 parents 220ba97 + eedd401 commit 468ccba

File tree

4 files changed

+949
-922
lines changed

4 files changed

+949
-922
lines changed

lib/type-helpers/partial-type.helper.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Type } from '@nestjs/common';
22
import {
33
applyIsOptionalDecorator,
4+
applyValidateIfDefinedDecorator,
45
inheritPropertyInitializers,
56
inheritTransformationMetadata,
67
inheritValidationMetadata
@@ -15,7 +16,25 @@ import { clonePluginMetadataFactory } from './mapped-types.utils';
1516

1617
const 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

Comments
 (0)