Skip to content

Commit 94679b2

Browse files
committed
feature(common): add transformOptions to ValidationPipeOptions
allows plainToClass to expose class properties to defined groups as per issue nestjs#1374
1 parent 83fc4c3 commit 94679b2

1 file changed

Lines changed: 27 additions & 12 deletions

File tree

packages/common/test/pipes/validation.pipe.spec.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as sinon from 'sinon';
22
import { expect } from 'chai';
33
import { ArgumentMetadata } from '../../interfaces';
4-
import { IsString } from 'class-validator';
4+
import { IsString, IsOptional } from 'class-validator';
55
import { ValidationPipe } from '../../pipes/validation.pipe';
66
import { Exclude, Expose } from 'class-transformer';
77

88
@Exclude()
9-
class TestModel {
9+
class TestModelInternal {
1010
constructor() {}
1111
@Expose()
1212
@IsString()
@@ -18,16 +18,29 @@ class TestModel {
1818

1919
@Expose({ groups: ['internal'] })
2020
@IsString()
21+
@IsOptional()
2122
public propInternal: string;
2223
}
2324

25+
class TestModel {
26+
constructor() {}
27+
@IsString() public prop1: string;
28+
29+
@IsString() public prop2: string;
30+
}
31+
2432
describe('ValidationPipe', () => {
2533
let target: ValidationPipe;
2634
const metadata: ArgumentMetadata = {
2735
type: 'body',
2836
metatype: TestModel,
2937
data: '',
3038
};
39+
const metadatainternal: ArgumentMetadata = {
40+
type: 'body',
41+
metatype: TestModelInternal,
42+
data: '',
43+
};
3144

3245
describe('transform', () => {
3346
describe('when validation passes', () => {
@@ -80,31 +93,33 @@ describe('ValidationPipe', () => {
8093
});
8194
describe('when transformation is internal', () => {
8295
it('should return a TestModel with internal property', async () => {
83-
target = new ValidationPipe({
84-
transformOptions: { groups: ['internal'] },
96+
target = new ValidationPipe({
97+
transform: true,
98+
transformOptions: { groups: ['internal'] }
8599
});
86100
const testObj = {
87101
prop1: 'value1',
88102
prop2: 'value2',
89-
propInternal: 'value3',
103+
propInternal: 'value3'
90104
};
91-
expect(await target.transform(testObj, metadata)).to.have.property(
92-
'propInternal',
93-
);
105+
expect(
106+
await target.transform(testObj, metadatainternal)
107+
).to.have.property('propInternal');
94108
});
95109
});
96110
describe('when transformation is external', () => {
97111
it('should return a TestModel without internal property', async () => {
98-
target = new ValidationPipe({
99-
transformOptions: { groups: ['external'] },
112+
target = new ValidationPipe({
113+
transform: true,
114+
transformOptions: { groups: ['external'] }
100115
});
101116
const testObj = {
102117
prop1: 'value1',
103118
prop2: 'value2',
104-
propInternal: 'value3',
119+
propInternal: 'value3'
105120
};
106121
expect(
107-
await target.transform(testObj, metadata),
122+
await target.transform(testObj, metadatainternal)
108123
).to.not.have.property('propInternal');
109124
});
110125
});

0 commit comments

Comments
 (0)