Skip to content

Commit

Permalink
feature(common): add transformOptions to ValidationPipeOptions
Browse files Browse the repository at this point in the history
allows plainToClass to expose class properties to defined groups as per issue nestjs#1374
  • Loading branch information
FionaLovett committed Jan 2, 2019
1 parent 83fc4c3 commit 94679b2
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions packages/common/test/pipes/validation.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as sinon from 'sinon';
import { expect } from 'chai';
import { ArgumentMetadata } from '../../interfaces';
import { IsString } from 'class-validator';
import { IsString, IsOptional } from 'class-validator';
import { ValidationPipe } from '../../pipes/validation.pipe';
import { Exclude, Expose } from 'class-transformer';

@Exclude()
class TestModel {
class TestModelInternal {
constructor() {}
@Expose()
@IsString()
Expand All @@ -18,16 +18,29 @@ class TestModel {

@Expose({ groups: ['internal'] })
@IsString()
@IsOptional()
public propInternal: string;
}

class TestModel {
constructor() {}
@IsString() public prop1: string;

@IsString() public prop2: string;
}

describe('ValidationPipe', () => {
let target: ValidationPipe;
const metadata: ArgumentMetadata = {
type: 'body',
metatype: TestModel,
data: '',
};
const metadatainternal: ArgumentMetadata = {
type: 'body',
metatype: TestModelInternal,
data: '',
};

describe('transform', () => {
describe('when validation passes', () => {
Expand Down Expand Up @@ -80,31 +93,33 @@ describe('ValidationPipe', () => {
});
describe('when transformation is internal', () => {
it('should return a TestModel with internal property', async () => {
target = new ValidationPipe({
transformOptions: { groups: ['internal'] },
target = new ValidationPipe({
transform: true,
transformOptions: { groups: ['internal'] }
});
const testObj = {
prop1: 'value1',
prop2: 'value2',
propInternal: 'value3',
propInternal: 'value3'
};
expect(await target.transform(testObj, metadata)).to.have.property(
'propInternal',
);
expect(
await target.transform(testObj, metadatainternal)
).to.have.property('propInternal');
});
});
describe('when transformation is external', () => {
it('should return a TestModel without internal property', async () => {
target = new ValidationPipe({
transformOptions: { groups: ['external'] },
target = new ValidationPipe({
transform: true,
transformOptions: { groups: ['external'] }
});
const testObj = {
prop1: 'value1',
prop2: 'value2',
propInternal: 'value3',
propInternal: 'value3'
};
expect(
await target.transform(testObj, metadata),
await target.transform(testObj, metadatainternal)
).to.not.have.property('propInternal');
});
});
Expand Down

0 comments on commit 94679b2

Please sign in to comment.