|
| 1 | +import { ValidationError, IsEmail } from 'class-validator'; |
| 2 | +import { transformAndValidate } from "../index"; |
| 3 | + |
| 4 | +import { expect } from "chai"; |
| 5 | +import * as chai from "chai"; |
| 6 | +import * as chaiAsPromised from "chai-as-promised"; |
| 7 | +chai.use(chaiAsPromised); |
| 8 | + |
| 9 | + |
| 10 | +class User { |
| 11 | + @IsEmail() |
| 12 | + public email: string; |
| 13 | + |
| 14 | + public greet(): string { |
| 15 | + return "Greeting"; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +describe("transformAndValidate()", () => { |
| 20 | + let user: User; |
| 21 | + const rejectMessage = "Incorrect object param type! Only strings and plain objects are valid."; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + user = { |
| 25 | + email: "test@test.com" |
| 26 | + } as User; |
| 27 | + }); |
| 28 | + |
| 29 | + it("should successfully transform and validate user plain object", async () => { |
| 30 | + const transformedUser = await transformAndValidate(User, user); |
| 31 | + expect(transformedUser).to.exist; |
| 32 | + expect(transformedUser.email).to.equals("test@test.com"); |
| 33 | + expect(transformedUser.greet()).to.equals("Greeting"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should successfully transform and validate user JSON", async () => { |
| 37 | + const userJson = JSON.stringify(user); |
| 38 | + |
| 39 | + const transformedUser = await transformAndValidate(User, userJson); |
| 40 | + expect(transformedUser).to.exist; |
| 41 | + expect(transformedUser.email).to.equals("test@test.com"); |
| 42 | + expect(transformedUser.greet()).to.equals("Greeting"); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should throw SyntaxError while parsing invalid JSON string", async () => { |
| 46 | + const userJson = JSON.stringify(user) + "error"; |
| 47 | + |
| 48 | + const error = await expect(transformAndValidate(User, userJson)).to.be.rejected; |
| 49 | + expect(error).to.be.instanceOf(SyntaxError); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should throw Error when object parameter is a number", async () => { |
| 53 | + const error: Error = await expect(transformAndValidate(User, 2 as any)).to.be.rejected; |
| 54 | + expect(error).to.exist; |
| 55 | + expect(error.message).to.equals(rejectMessage); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should throw Error when object parameter is a function", async () => { |
| 59 | + const func = () => ({ email: "test@test.com"}); |
| 60 | + |
| 61 | + const error: Error = await expect(transformAndValidate(User, func)).to.be.rejected; |
| 62 | + expect(error).to.exist; |
| 63 | + expect(error.message).to.equals(rejectMessage); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should throw Error when object parameter is a boolean value", async () => { |
| 67 | + const error: Error = await expect(transformAndValidate(User, true as any)).to.be.rejected; |
| 68 | + expect(error).to.exist; |
| 69 | + expect(error.message).to.equals(rejectMessage); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should throw ValidationError array when object property is not passing validation", async () => { |
| 73 | + const user = { |
| 74 | + email: "test@test" |
| 75 | + } as User; |
| 76 | + |
| 77 | + const error = await expect(transformAndValidate(User, user)).to.be.rejected; |
| 78 | + expect(error).to.have.lengthOf(1); |
| 79 | + expect(error[0]).to.be.instanceOf(ValidationError); |
| 80 | + }); |
| 81 | +}); |
0 commit comments