Skip to content

Commit

Permalink
fix(): mapped types with renamed fields
Browse files Browse the repository at this point in the history
  • Loading branch information
andreialecu committed Sep 15, 2020
1 parent 93e2a8e commit c10360c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/type-helpers/omit-type.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function OmitType<T, K extends keyof T>(
inheritTransformationMetadata(classRef, OmitObjectType, isInheritedPredicate);

fields
.filter((item) => !keys.includes(item.schemaName as K))
.filter((item) => !keys.includes(item.name as K))
.forEach((item) => {
if (isFunction(item.typeFn)) {
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/type-helpers/pick-type.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function PickType<T, K extends keyof T>(
inheritTransformationMetadata(classRef, PickObjectType, isInheritedPredicate);

fields
.filter((item) => keys.includes(item.schemaName as K))
.filter((item) => keys.includes(item.name as K))
.forEach((item) => {
if (isFunction(item.typeFn)) {
/**
Expand Down
7 changes: 5 additions & 2 deletions tests/type-helpers/omit-type.helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ describe('OmitType', () => {
@MinLength(10)
@Field()
password: string;

@Field({ name: 'id' })
_id: string;
}

class UpdateUserDto extends OmitType(CreateUserDto, ['login']) {}
class UpdateUserDto extends OmitType(CreateUserDto, ['login', '_id']) {}

it('should inherit all fields except for "login"', () => {
it('should inherit all fields except for "login" and "_id"', () => {
const { fields } = getFieldsAndDecoratorForType(
Object.getPrototypeOf(UpdateUserDto),
);
Expand Down
13 changes: 13 additions & 0 deletions tests/type-helpers/pick-type.helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,28 @@ describe('PickType', () => {
@MinLength(10)
@Field()
password: string;

@Field({ name: 'id' })
_id: string;
}

class UpdateUserDto extends PickType(CreateUserDto, ['login']) {}

class UpdateUserWithIdDto extends PickType(CreateUserDto, ['_id']) {}

it('should inherit "login" field', () => {
const { fields } = getFieldsAndDecoratorForType(
Object.getPrototypeOf(UpdateUserDto),
);
expect(fields.length).toEqual(1);
expect(fields[0].name).toEqual('login');
});

it('should inherit renamed "_id" field', () => {
const { fields } = getFieldsAndDecoratorForType(
Object.getPrototypeOf(UpdateUserWithIdDto),
);
expect(fields.length).toEqual(1);
expect(fields[0].name).toEqual('_id');
});
});

0 comments on commit c10360c

Please sign in to comment.