-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { HttpException } from "@nestjs/core"; | ||
import { | ||
PipeTransform, | ||
Pipe, | ||
ArgumentMetadata, | ||
HttpStatus | ||
} from "@nestjs/common"; | ||
import { validate } from "class-validator"; | ||
import { plainToClass } from "class-transformer"; | ||
|
||
@Pipe() | ||
export class PipesModelParser implements PipeTransform<any> { | ||
async transform(value, metadata: ArgumentMetadata) { | ||
const { metatype } = metadata; | ||
if (!metatype || !this.toValidate(metatype)) { | ||
return value; | ||
} | ||
const entity = plainToClass(metatype, value); | ||
const errors = await validate(entity); | ||
if (errors.length > 0) { | ||
throw new HttpException("Validation failed", HttpStatus.BAD_REQUEST); | ||
} | ||
return entity; | ||
} | ||
|
||
private toValidate(metatype): boolean { | ||
const types = [String, Boolean, Number, Array, Object]; | ||
return !types.find(type => metatype === type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as sinon from "sinon"; | ||
import { expect } from "chai"; | ||
import { PipesModelParser } from "./../../pipes/pipes-model-parser"; | ||
import { ArgumentMetadata } from "@nestjs/common"; | ||
import { IsString } from "class-validator"; | ||
|
||
class TestModel { | ||
constructor() {} | ||
@IsString() | ||
public prop1: string; | ||
|
||
@IsString() | ||
public prop2: string; | ||
} | ||
|
||
describe("PipesModelParser", () => { | ||
let target: PipesModelParser; | ||
let testModel; | ||
const metadata: ArgumentMetadata = { | ||
type: "body", | ||
metatype: TestModel, | ||
data: "" | ||
}; | ||
beforeEach(() => { | ||
target = new PipesModelParser(); | ||
}); | ||
describe("transform", () => { | ||
describe("when metadata is empty or undefined", () => { | ||
it("should return the value unchanged", async () => { | ||
const testObj = { prop1: "value1", prop2: "value2" }; | ||
expect(await target.transform(testObj, {} as any)).to.equal(testObj); | ||
expect(await target.transform(testObj, {} as any)).to.not.be.instanceOf( | ||
TestModel | ||
); | ||
}); | ||
}); | ||
describe("when metadata contains a class", () => { | ||
it("should return an instance of the class", async () => { | ||
const testObj = { prop1: "value1", prop2: "value2" }; | ||
const result = await target.transform(testObj, metadata); | ||
expect(result).to.be.instanceOf(TestModel); | ||
}); | ||
}); | ||
describe("when validation vails", () => { | ||
it("should throw an error", async () => { | ||
const testObj = { prop1: "value1" }; | ||
return expect(target.transform(testObj, metadata)).to.be.rejected; | ||
}); | ||
}); | ||
}); | ||
}); |