Skip to content

Commit

Permalink
fix(oak): origin Cls should also validate
Browse files Browse the repository at this point in the history
  • Loading branch information
jiawei397 committed May 25, 2022
1 parent 6a6732d commit 6d48105
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 26 deletions.
8 changes: 2 additions & 6 deletions src/decorators/oak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,11 @@ export function getTransNumOrBool(type: Constructor, val: string) {
return val;
}

async function transAndValidateByCls(
export async function transAndValidateByCls(
cls: Constructor,
map: Record<string, string | number | boolean>,
) {
const keys = Reflect.getMetadataKeys(cls.prototype);
let isNeedValidate = false;
keys.forEach((key) => {
if (!key.startsWith(typePreKey)) {
return;
Expand All @@ -135,15 +134,12 @@ async function transAndValidateByCls(
if (map[realKey] === undefined) {
return;
}
isNeedValidate = true;
map[realKey] = getTransNumOrBool(
type,
map[realKey] as string,
);
});
if (isNeedValidate) { // if not use Property to translate the params, then we can skip this
await validateParams(cls, map);
}
await validateParams(cls, map);
return map;
}

Expand Down
107 changes: 87 additions & 20 deletions src/decorators/oak_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {
assert,
assertEquals,
IsNumber,
IsString,
Max,
Min,
Expand All @@ -22,6 +23,7 @@ import {
Query,
Req,
Res,
transAndValidateByCls,
UploadedFile,
} from "./oak.ts";

Expand Down Expand Up @@ -176,7 +178,7 @@ Deno.test("body", async () => {
}
});

Deno.test("Query", async () => {
Deno.test("Query", async (t) => {
const callStack: number[] = [];
const mockQuery = {
a: "b",
Expand All @@ -193,6 +195,7 @@ Deno.test("Query", async () => {
a: "b",
d: "30",
};
const mockErrorValidatePath = "/f?a=b&d=30";

// deno-lint-ignore no-unused-vars
class QueryDto {
Expand Down Expand Up @@ -223,6 +226,13 @@ Deno.test("Query", async () => {
class QueryNotValidateDto {
a!: string;

d!: number;
}

// deno-lint-ignore no-unused-vars
class QueryValidateErrorDto {
a!: string;

@Max(20)
d!: number;
}
Expand Down Expand Up @@ -308,12 +318,17 @@ Deno.test("Query", async () => {
"not set Property, so should be string type",
);
}

@Get("f")
testErrorValidateQuery(@Query() _query: QueryValidateErrorDto) {
assert(false, "should not be here");
}
}

const router = new Router();
await router.add(A);

{
await t.step("get mock", async () => {
const ctx = testing.createMockContext({
path: mockPath,
method: "GET",
Expand All @@ -325,9 +340,9 @@ Deno.test("Query", async () => {

assertEquals(callStack, [1]);
callStack.length = 0;
}
});

{
await t.step("post mock", async () => {
const ctx = testing.createMockContext({
method: "POST",
path: mockPath,
Expand All @@ -339,9 +354,9 @@ Deno.test("Query", async () => {

assertEquals(callStack, [2]);
callStack.length = 0;
}
});

{
await t.step("get b", async () => {
const ctx = testing.createMockContext({
path: "/b",
method: "GET",
Expand All @@ -353,9 +368,9 @@ Deno.test("Query", async () => {

assertEquals(callStack, [3]);
callStack.length = 0;
}
});

{
await t.step("get error", async () => {
const ctx = testing.createMockContext({
path: mockErrorPath,
method: "GET",
Expand All @@ -368,13 +383,13 @@ Deno.test("Query", async () => {
} catch (error) {
// console.log(error);
assertEquals(error.message, "c must not be greater than 20");
callStack.push(5);
callStack.push(6);
}
assertEquals(callStack, [5]);
assertEquals(callStack, [6]);
callStack.length = 0;
}
});

{
await t.step("get error but not validate path", async () => {
const ctx = testing.createMockContext({
path: mockErrorButNotValidatePath,
method: "GET",
Expand All @@ -384,7 +399,25 @@ Deno.test("Query", async () => {
await mw(ctx, next);
assertEquals(callStack, [5]);
callStack.length = 0;
}
});

await t.step("get validate path", async () => {
const ctx = testing.createMockContext({
path: mockErrorValidatePath,
method: "GET",
});
const mw = router.routes();
const next = testing.createMockNext();
try {
await mw(ctx, next);
} catch (error) {
// console.log(error);
assertEquals(error.message, "d must not be greater than 20");
callStack.push(7);
}
assertEquals(callStack, [7]);
callStack.length = 0;
});
});

Deno.test("Params", async () => {
Expand Down Expand Up @@ -565,7 +598,7 @@ Deno.test("Headers", async () => {
callStack.length = 0;
});

Deno.test("UploadedFile", async () => {
Deno.test("UploadedFile form data", async (t) => {
const callStack: number[] = [];
const fileMockData = {
fields: { test: "a" },
Expand All @@ -585,20 +618,20 @@ Deno.test("UploadedFile", async () => {
@Post("a")
noUpload(@UploadedFile() body: any) {
callStack.push(1);
assertEquals(body, undefined, "get will not pass body");
assertEquals(body, undefined, "no upload data will not pass body");
}

@Post("b")
upload(@UploadedFile() body: any) {
callStack.push(2);
assertEquals(body, fileMockData, "get will pass body");
assertEquals(body, fileMockData, "will pass body");
}
}

const router = new Router();
await router.add(A);

{
await t.step("not upload", async () => {
const ctx = createMockContext({
path: "/a",
method: "POST",
Expand All @@ -618,9 +651,9 @@ Deno.test("UploadedFile", async () => {
assertEquals(callStack, [1]);

callStack.length = 0;
}
});

{
await t.step("upload", async () => {
const ctx = createMockContext({
path: "/b",
method: "POST",
Expand All @@ -638,5 +671,39 @@ Deno.test("UploadedFile", async () => {

assertEquals(callStack, [2]);
callStack.length = 0;
}
});
});

Deno.test("transAndValidateByCls", async (t) => {
await t.step("not trans", async () => {
class A {
@IsNumber()
age: number;
}
try {
await transAndValidateByCls(A, {
age: "12",
});
assert(false, "should not reach here");
} catch (error) {
assertEquals(
error.message,
"age must be a number conforming to the specified constraints",
);
}
});

await t.step("trans", async () => {
class A {
@IsNumber()
@Property()
age: number;
}
const result = await transAndValidateByCls(A, {
age: "12",
});
assertEquals(result, {
age: 12,
});
});
});
1 change: 1 addition & 0 deletions test_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type {
} from "https://deno.land/x/[email protected]/mod.ts";

export {
IsNumber,
IsOptional,
IsString,
Max,
Expand Down

0 comments on commit 6d48105

Please sign in to comment.