Skip to content

Commit e0ed33f

Browse files
committed
Added tests + updated package.json
1 parent acbb2dd commit e0ed33f

File tree

5 files changed

+116
-7
lines changed

5 files changed

+116
-7
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
build
3-
.vscode
3+
.vscode
4+
package

package.json

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "class-transformer-validator",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "A simple wrapper around class-transformer and class-validator which provides nice and programmer-friendly API.",
55
"license": "MIT",
66
"readmeFilename": "README.md",
@@ -17,11 +17,37 @@
1717
},
1818
"tags": [
1919
"class-validator",
20+
"validator",
21+
"validation",
22+
"typescript",
23+
"typescript-validator",
2024
"class-transformer",
21-
"validation"
25+
"serialization",
26+
"deserialization",
27+
"serializer",
28+
"typescript",
29+
"object-to-class",
30+
"typescript-serializer"
2231
],
32+
"devDependencies": {
33+
"@types/chai": "^3.4.34",
34+
"@types/chai-as-promised": "0.0.29",
35+
"@types/mocha": "^2.2.38",
36+
"chai": "^3.5.0",
37+
"chai-as-promised": "^6.0.0",
38+
"class-transformer": "^0.1.6",
39+
"class-validator": "^0.6.8",
40+
"mocha": "^3.2.0",
41+
"typescript": "^2.1.5"
42+
},
43+
"peerDependencies": {
44+
"class-validator": "^0.6.8",
45+
"class-transformer": "^0.1.6"
46+
},
2347
"main": "index.js",
2448
"scripts": {
25-
"test": "echo \"Error: no test specified\" && exit 1"
49+
"pretest": "npm run build",
50+
"test": "mocha build/tests/index.js",
51+
"build": "tsc"
2652
}
2753
}

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function transformAndValidate<T extends Object>(classType: ClassType<T>,
3636
} else if (typeof objectOrString === "object") {
3737
object = objectOrString;
3838
} else {
39-
return reject("Incorrect object param type! Only strings and plain objects are valid.");
39+
return reject(new Error("Incorrect object param type! Only strings and plain objects are valid."));
4040
}
4141

4242
const classObject = plainToClass(classType, object, options ? options.transformer : undefined);

src/tests/index.ts

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
});

tsconfig.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"experimentalDecorators": true,
66
"declaration": true,
77
"forceConsistentCasingInFileNames": true,
8+
"lib": ["es5", "es2015.promise"],
89
"moduleResolution": "node",
910
"module": "commonjs",
1011
"noImplicitAny": true,
@@ -15,7 +16,7 @@
1516
"outDir": "./build",
1617
"rootDir": "./src",
1718
"sourceMap": true,
18-
"target": "es6",
19+
"target": "es5",
1920
"strictNullChecks": true,
2021
"suppressImplicitAnyIndexErrors": false,
2122
"removeComments": true,
@@ -25,4 +26,4 @@
2526
"node_modules",
2627
"build"
2728
]
28-
}
29+
}

0 commit comments

Comments
 (0)