Skip to content

Commit

Permalink
Merge branch 'nestjs:master' into feature/add-response-example
Browse files Browse the repository at this point in the history
  • Loading branch information
lit26 authored Apr 3, 2024
2 parents e5adbe9 + eedd401 commit 8e7208f
Show file tree
Hide file tree
Showing 4 changed files with 949 additions and 922 deletions.
27 changes: 23 additions & 4 deletions lib/type-helpers/partial-type.helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Type } from '@nestjs/common';
import {
applyIsOptionalDecorator,
applyValidateIfDefinedDecorator,
inheritPropertyInitializers,
inheritTransformationMetadata,
inheritValidationMetadata
Expand All @@ -15,7 +16,25 @@ import { clonePluginMetadataFactory } from './mapped-types.utils';

const modelPropertiesAccessor = new ModelPropertiesAccessor();

export function PartialType<T>(classRef: Type<T>): Type<Partial<T>> {
export function PartialType<T>(
classRef: Type<T>,
/**
* Configuration options.
*/
options: {
/**
* If true, validations will be ignored on a property if it is either null or undefined. If
* false, validations will be ignored only if the property is undefined.
* @default true
*/
skipNullProperties?: boolean;
} = {}
): Type<Partial<T>> {
const applyPartialDecoratorFn =
options.skipNullProperties === false
? applyValidateIfDefinedDecorator
: applyIsOptionalDecorator;

const fields = modelPropertiesAccessor.getModelProperties(classRef.prototype);

abstract class PartialTypeClass {
Expand All @@ -30,7 +49,7 @@ export function PartialType<T>(classRef: Type<T>): Type<Partial<T>> {
if (keysWithValidationConstraints) {
keysWithValidationConstraints
.filter((key) => !fields.includes(key))
.forEach((key) => applyIsOptionalDecorator(PartialTypeClass, key));
.forEach((key) => applyPartialDecoratorFn(PartialTypeClass, key));
}

inheritTransformationMetadata(classRef, PartialTypeClass);
Expand All @@ -48,7 +67,7 @@ export function PartialType<T>(classRef: Type<T>): Type<Partial<T>> {
PartialTypeClass[METADATA_FACTORY_NAME]()
);
pluginFields.forEach((key) =>
applyIsOptionalDecorator(PartialTypeClass, key)
applyPartialDecoratorFn(PartialTypeClass, key)
);
}

Expand All @@ -65,7 +84,7 @@ export function PartialType<T>(classRef: Type<T>): Type<Partial<T>> {
required: false
});
decoratorFactory(PartialTypeClass.prototype, key);
applyIsOptionalDecorator(PartialTypeClass, key);
applyPartialDecoratorFn(PartialTypeClass, key);
});
}
applyFields(fields);
Expand Down
Loading

0 comments on commit 8e7208f

Please sign in to comment.